Room类
属性:房号、面积、地址
方法:getDetails 返回String,
描述房间详细信息
构造器:0-3个参数的重载构造器,使用this()调用
使用封装
TestRoom类
分别用4个构造器创建4个room对象,打印对象的详细信息
才开始学,java的一个编程求代码被难住了
答案:2 悬赏:70 手机版
解决时间 2021-03-08 16:38
- 提问者网友:欲劫无渡
- 2021-03-08 01:25
最佳答案
- 五星知识达人网友:蕴藏春秋
- 2021-03-08 02:16
public class TestRoom {
public static void main(String[] args) {
Room r1 = new Room();
System.out.println(r1.getDetails());
Room r2 = new Room(1);
System.out.println(r2.getDetails());
Room r3 = new Room(2,88.8);
System.out.println(r3.getDetails());
Room r4 = new Room(3,76.9,"地球");
System.out.println(r4.getDetails());
}
}
class Room {
private int roomNo;
private double area;
private String address;
public Room(int roomNo, double area, String address) {
super();
this.roomNo = roomNo;
this.area = area;
this.address = address;
}
public Room() {
}
public Room(int roomNo, double area){
this(roomNo, area, null);
}
public Room(int roomNo) {
this(roomNo, 0, null);
}
public String getDetails(){
return "roomNo:"+roomNo+",area:"+area+",address:"+address;
}
}
public static void main(String[] args) {
Room r1 = new Room();
System.out.println(r1.getDetails());
Room r2 = new Room(1);
System.out.println(r2.getDetails());
Room r3 = new Room(2,88.8);
System.out.println(r3.getDetails());
Room r4 = new Room(3,76.9,"地球");
System.out.println(r4.getDetails());
}
}
class Room {
private int roomNo;
private double area;
private String address;
public Room(int roomNo, double area, String address) {
super();
this.roomNo = roomNo;
this.area = area;
this.address = address;
}
public Room() {
}
public Room(int roomNo, double area){
this(roomNo, area, null);
}
public Room(int roomNo) {
this(roomNo, 0, null);
}
public String getDetails(){
return "roomNo:"+roomNo+",area:"+area+",address:"+address;
}
}
全部回答
- 1楼网友:青尢
- 2021-03-08 03:42
学java,用eclipse吧
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯