[英]Accessing a variable in another object of the same class
我正在尝试访问“其他”对象中的数组,但无法弄清楚如何访问变量。 这是我到目前为止的内容:
public void union(DataSet other)
{
DataSet temp = new newdataexp();
temp = other;
}
即使我在类中创建了一个返回变量的方法,然后尝试从此方法联合调用它的方法,也无法弄清楚如何访问该变量。
我有这个方法,正在尝试做:String [] [] temp = other.getdata(),但是编译器说它找不到符号:方法getdata()。
公共字符串[] [] getdata(){
返回文件数据;
}
我不知道是否能很好地理解您的问题,但是要尝试访问同一类的另一个对象中的变量,请尝试以下操作:
class MyClass {
private int[] myArray = new int[10];
public void myMethod(MyClass myClass) {
// you can in this way:
// int[] tempArray = myClass.myArray
// but this is better:
int[] tempArray = myClass.getMyArray();
}
public int[] getMyArray() {
return myArray;
}
}
编辑:
但是,如果要使联合更好地在类外部提取联合方法,请创建方法:
public static MyObject union(MyObject myObjectFirst, MyObject myObjectSecond) {
...
}
只要方法返回,就用String,int等替换数据类型。
datatype mynewdata = (datatype)other.getMeMyArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.