java定义一个房子house类,类中需要
时间: 2024-05-05 13:16:51 浏览: 77
包含以下属性和方法:
属性:
- 地址(address)
- 面积(area)
- 房间数量(roomNum)
- 是否有花园(hasGarden)
方法:
- 构造方法,可以传入地址、面积、房间数量和是否有花园来初始化对象
- 获取地址的方法(getAddress)
- 获取面积的方法(getArea)
- 获取房间数量的方法(getRoomNum)
- 获取是否有花园的方法(getHasGarden)
- 修改地址的方法(setAddress)
- 修改面积的方法(setArea)
- 修改房间数量的方法(setRoomNum)
- 修改是否有花园的方法(setHasGarden)
代码示例:
```
public class House {
private String address;
private double area;
private int roomNum;
private boolean hasGarden;
public House(String address, double area, int roomNum, boolean hasGarden) {
this.address = address;
this.area = area;
this.roomNum = roomNum;
this.hasGarden = hasGarden;
}
public String getAddress() {
return address;
}
public double getArea() {
return area;
}
public int getRoomNum() {
return roomNum;
}
public boolean getHasGarden() {
return hasGarden;
}
public void setAddress(String address) {
this.address = address;
}
public void setArea(double area) {
this.area = area;
}
public void setRoomNum(int roomNum) {
this.roomNum = roomNum;
}
public void setHasGarden(boolean hasGarden) {
this.hasGarden = hasGarden;
}
}
```
阅读全文