【发布时间】:2015-05-28 11:21:38
【问题描述】:
这是我的数组:
public class TestProgram {
public static final Room[] rooms = new Room[]
{
new Room ("GARDEN0001", "NorthWest Garden View", 45.0),
new PremiumRoom ("POOL0001", "Poolside Terrace", 90.0, 1, 150, 50),
new Room ("GARDEN0003", "North Garden View", 35.0),
new Room ("GARDEN0005", "West Garden View", 35.0),
new PremiumRoom ("POOL0002", "Poolside Private", 125.0, 2, 100, 75),
new Room ("GARDEN0004", "South Garden View", 52.0)
};
}
这是我的方法:
private static void displayrooms() {
rooms[0].print();
System.out.println();
rooms[1].print();
System.out.println();
rooms[2].print();
System.out.println();
rooms[3].print();
System.out.println();
rooms[4].print();
System.out.println();
rooms[5].print();
}
但是,当它打印出列表时,它会跳过我的 PremiumRooms 对象。我该如何解决这个问题?高级房间是房间的子类。
这是它输出的内容,缺少 2 个 premiumroom 对象:
房间号:GARDEN0001 说明:西北园景 每日费率:45.0 状态:A 房间号:GARDEN0003 说明:北园景 每日费率:35.0 状态:A 房间号:GARDEN0005 说明:西园景 每日费率:35.0 状态:A 房间号:GARDEN0004 说明:南园景 每日费率:52.0 状态:A你们中的一些人要求提供 Room 和 PremiumRoom 代码。他们在这里:
public Room(String roomId, String description, double dailyRate)
{
this.roomId = roomId;
this.description = description;
this.dailyRate = dailyRate;
this.status = 'A';
}
public PremiumRoom(String roomId, String description, double dailyRate, int freeNights, double discountRate, double nextBookingDiscountVoucher)
{
super(roomId, description, dailyRate);
this.freeNights = freeNights;
this.discountRate = discountRate;
this.nextBookingDiscountVoucher = nextBookingDiscountVoucher;
}
【问题讨论】:
-
分享
PremiumRoom类源码? -
请显示
print方法的代码以及Room和PremiumRoom类的代码 -
(如何)
PremiumRoom.print()是如何实现的?此外,您至少要执行 5xprintln(),因此您将有 5 个换行符,即两个空行。如果不是,我们会看到错误的代码。 -
我们没有要求构造函数的代码。我们询问了类的代码,特别是字段定义和
print方法是相关的,以及该代码引用的任何其他内容。 -
打印方法在哪里?
标签: java arrays object methods subclass