-1
callingmethod(){
File f=new File();  
//...
String s= new String();
//...

method( f + s);    // here is problem (most put f+s in object to send it to method)
}

メソッドの引数を変更できない

method(Object o){
//...
//how to split it to file and String here 
}

不明な点がある場合は、plz にお問い合わせください

4

2 に答える 2

3

最もクリーンで慣用的な方法は、ペアを表す単純なクラスを作成することです。

static class FileString {
  public final File f;
  public final String s;
  FileString(File f, String s) { 
    this.f = f; this.s = s;
  }
}

それから書く

method(new FileString(file, string));

内部メソッド:

FileString fs = (FileString)o;
// use fs.f and fs.s

詳細に応じて、私の例のようにネストされたクラスを使用するか、それを独自のファイルに入れます。インスタンス化する場所の近くに置くと、私が行ったように、コンストラクターをプライベートまたはパッケージプライベートにすることができます。しかし、これらは細かい部分にすぎません。

于 2012-11-22T22:00:05.563 に答える
1

たとえば、配列に入れることができます:

method (new Object[] {f, s});

void method (Object o) {
    final Object[] arr = (Object[]) o;
    File f = (File) arr[0];
    String s = (String) arr[1];
}
于 2012-11-22T21:57:46.187 に答える