简体   繁体   English

访问同一类的另一个对象中的变量

[英]Accessing a variable in another object of the same class

I'm trying to access an array in the object "other," but I can't figure out how to access the variable. 我正在尝试访问“其他”对象中的数组,但无法弄清楚如何访问变量。 This is what I have so far: 这是我到目前为止的内容:

public void union(DataSet other)
{  
  DataSet temp = new newdataexp();
  temp = other; 
}

I haven't been able to figure out how to access the variable, even when I create a method in the class that returns the variable and then trying to call it from this method union. 即使我在类中创建了一个返回变量的方法,然后尝试从此方法联合调用它的方法,也无法弄清楚如何访问该变量。

I have this method, and am trying to do: String[][] temp = other.getdata(), but the compiler says that it cannot find symbol: method getdata(). 我有这个方法,正在尝试做:String [] [] temp = other.getdata(),但是编译器说它找不到符号:方法getdata()。

public String[][] getdata() { 公共字符串[] [] getdata(){

return filedata; 返回文件数据;

} }

I don't know if I good understand you question but to access a variable in another object of the same class just try this: 我不知道是否能很好地理解您的问题,但是要尝试访问同一类的另一个对象中的变量,请尝试以下操作:

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

}

Edited: 编辑:

But if you want to make union better extract union method outside class, create method: 但是,如果要使联合更好地在类外部提取联合方法,请创建方法:

public static MyObject union(MyObject myObjectFirst, MyObject myObjectSecond) {
   ...
}

只要方法返回,就用String,int等替换数据类型。

datatype mynewdata = (datatype)other.getMeMyArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM