当前位置: 代码迷 >> J2EE >> java的构造方法求解,该如何处理
  详细解决方案

java的构造方法求解,该如何处理

热度:121   发布时间:2016-04-22 02:34:30.0
java的构造方法求解
public Hotel(String name, String address) {
super();
this.name = name;
this.address = address;
this.rooms = new Room[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
rooms[i][j].setGuestName("EMPTY");
rooms[i][j].setId(i * 100 + j);
}
}

请问构造方法能不能这样写??

------解决方案--------------------
根据你给的部分代码,我尝试写了下,看看是不是你想要的答案

Java code
public class Room {    private String guestName;    private int id;    public String getGuestName() {        return guestName;    }    public void setGuestName(String guestName) {        this.guestName = guestName;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }}public class Hotel {    private String name;    private String address;    public static int m = 10;    public static int n = 5;    private Room[][] rooms;    public Hotel(String name, String address) {        super();        this.name = name;        this.address = address;        this.rooms = new Room[m][n];        for (int i = 0; i < m; i++) {            for (int j = 0; j < n; j++) {                rooms[i][j].setGuestName("EMPTY");                rooms[i][j].setId(i * 100 + j);            }        }    }}
------解决方案--------------------
语法来说。。当然可以那么写。。。毋庸置疑!!!
------解决方案--------------------
对的。。。做初始化嘛
  相关解决方案