【发布时间】:2016-03-10 19:53:51
【问题描述】:
我想从对象初始化的类中访问对象的变量。我尝试过使用 objectName.atrr 并尝试过使用 get 方法。当我键入“testObject”时使用这两种方法。属性不会显示在可能性列表中,get 方法也不会显示为选项。 如何在同一个类中访问 List 内的对象的属性?
public class State{
// This is the constuctor
Point sPos;
int sID;
int sRot;
List listState = new ArrayList();
public ClassName(Point shape_Pos, int shape_ID, int shape_Rot){
sPos = shape_Pos;
sID = shape_ID;
sRot = shape_Rot;
}
//here are the get methods
public int getID() {
return sID;
}
public Point getPos() {
return sPos;
}
public int getRot(){
return sRot;
}
public void methodName(){
//here is code that creates States and add it to listState (left it out)
currentState.add(new State(new Point(0,0),1,1));
...
Object testObject = currentState.get(0);
System.out.println("testObject = "+testObject);
//This is not working
int id = testObject.sID;
// This is also not working
int id2 = testObject.getID();
}
}
OUTPUT: // 正如我所期望和想要的,与列表中的对象相同
Object = State@1ae73783
【问题讨论】:
标签: java class object constructor attributes