简体   繁体   中英

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().

public String[][] 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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