如何解决访问同一类别的另一个对象中的变量
| 我正在尝试访问对象“其他”中的数组,但是我不知道如何访问变量。这是我到目前为止的内容:public void union(DataSet other)
{
DataSet temp = new newdataexp();
temp = other;
}
我无法弄清楚如何访问变量,即使我在类中创建了一个返回变量的方法,然后尝试从此方法联合调用它的时候。
我有这个方法,正在尝试做:String [] [] temp = other.getdata(),但是编译器说它找不到符号:方法getdata()。
公共字符串[] [] getdata()
{
返回文件数据;
}
解决方法
只要方法返回,就用String,int等替换数据类型。
datatype mynewdata = (datatype)other.getMeMyArray();
,我不知道是否能很好地理解您的问题,但是要尝试访问同一类的另一个对象中的变量,请尝试以下操作:
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) {
...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。