Java 如何在一个对象中合并两种不同的对象类型?
无法更改方法参数Java 如何在一个对象中合并两种不同的对象类型?,java,arrays,object,casting,Java,Arrays,Object,Casting,无法更改方法参数 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) } 对于任何不清楚的内容,请询问plz您可以将其放入阵列中: method(Object o){ //... //how to split it to file
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)
}
对于任何不清楚的内容,请询问plz您可以将其放入阵列中:
method(Object o){
//...
//how to split it to file and String here
}
最干净、最惯用的方法是创建一个简单的类来表示您的配对:
method (new Object[] {f, s});
void method (Object o) {
final Object[] arr = (Object[]) o;
File f = (File) arr[0];
String s = (String) arr[1];
}
然后写
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