开发者

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;

}


replace datatype with String, int, etc for whtever the method returns.

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


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) {
   ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜