Java访问实例变量

时间:2012-03-04 21:41:52

标签: java static static-methods

如何在最终调用retrieveRoom()的方法使静态方法可以访问名为rooms的对象数组;我试过公共静态客房[] =新房间[3]。但我只是从中得到错误。任何帮助表示赞赏。

public class MasterControlPanel{

    public static void main(String[] args){

    Rooms rooms[] = new Rooms[3];

    rooms[0] = new Rooms("Room U", 1, 4, 4);
    rooms[1] = new Rooms("Room U", 2, 4, 4);
    rooms[2] = new Rooms("Connector X", 3, 2, 4);
    rooms[3] = new Rooms("Connector U", 4, 2, 4);

    for(int x = 0; x <= rooms.length; x++){
        rooms[x].createLights();
        rooms[x].createWalls();
    }
    }

    public static Object retrieveRoom(int connectedRoom){

    connectedRoom -= 1;
    return rooms[connectedRoom];
    }

}

1 个答案:

答案 0 :(得分:3)

移动

public static Rooms[] rooms = new Rooms[4];

从你的main()方法,到这样的声明部分

public class MasterControlPanel {
    public static Rooms[] rooms = new Rooms[4];
    public static void main(String[] args) {
    ...
    }
}

这将在您的rooms课程中定义MasterControlPanel 静态成员

以下是其他一些提示:

  1. 只要存在Class,就可以访问静态成员。但是,为了访问实例成员(属于该类的实例的变量),您需要该类的实例。
  2. 使用Rooms[] rooms = new Rooms[4];。请注意,数组括号移动到Type部分,这样可以防止将来出现混淆。 “让我们定义一个名为rooms 的房间数组”。但这并不是一件好事。
  3. 您正在定义大小为 3 rooms数组,但是您要为其添加 4 元素。这将导致ArrayIndexOutOfBounds异常。
  4. 您应该让您的会员private达到Encapsulation。您可以提供publicprotected方法从您的课程中检索这些private成员。
  5. 当一个数组包含n个元素时,你可以在该数组上进行的最大连续迭代次数也是n,这是肯定的。这意味着在for循环中确保最后一次迭代的循环条件为n-1。请记住,大小4数组中没有4个元素。
  6. 以下是代码

    public class MasterControlPanel {
        public static Room[] rooms = new Room[4];
    
        public static void main(String[] args) {
    
            rooms[0] = new Room("Room U", 1, 4, 4);
            rooms[1] = new Room("Room U", 2, 4, 4);
            rooms[2] = new Room("Connector X", 3, 2, 4);
            rooms[3] = new Room("Connector U", 4, 2, 4);
    
            for (int x = 0; x <= rooms.length-1; x++) {
                rooms[x].createLights();
                rooms[x].createWalls();
            }
        }
    
        public static Room retrieveRoom(int connectedRoom) {
            connectedRoom -= 1;
            return rooms[connectedRoom];
        }
    
    }
    
    class Room {
        // These are instance members for Room class
        private String roomString;
        private int roomInteger1, roomInteger2, roomInteger3;
    
        public Room(String string, int integer1, int integer2, int integer3) {
            // This is the constructor for Room object
            this.roomString = string;
            this.roomInteger1 = integer1;
            this.roomInteger2 = integer2;
            this.roomInteger3 = integer3;
        }
    
        public void createLights() {
            // This method creates lights
        }
    
        public void createWalls() {
            // This method creates walls
        }
    
        // These are Getters and Setters for Room members
        public String getRoomString() {
            return roomString;
        }
    
        public void setRoomString(String roomString) {
            this.roomString = roomString;
        }
    
        public int getRoomInteger1() {
            return roomInteger1;
        }
    
        public void setRoomInteger1(int roomInteger1) {
            this.roomInteger1 = roomInteger1;
        }
    
        public int getRoomInteger2() {
            return roomInteger2;
        }
    
        public void setRoomInteger2(int roomInteger2) {
            this.roomInteger2 = roomInteger2;
        }
    
        public int getRoomInteger3() {
            return roomInteger3;
        }
    
        public void setRoomInteger3(int roomInteger3) {
            this.roomInteger3 = roomInteger3;
        }
    }