2012-11-22 145 views

回答

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]; 
} 
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 

依賴進一步的細節,使用像我的例子中的嵌套類,或將其放入它自己的文件。如果你保持它靠近實例化它的地方,那麼你可以像我一樣使構造函數是私有的或私有的。但這些只是更細緻的細節。

+0

感謝您的回答 –