这是我的阵列:
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对象:
Room ID: GARDEN0001 Description: NorthWest Garden View Daily-Rate: 45.0 Status: A Room ID: GARDEN0003 Description: North Garden View Daily-Rate: 35.0 Status: A Room ID: GARDEN0005 Description: West Garden View Daily-Rate: 35.0 Status: A Room ID: GARDEN0004 Description: South Garden View Daily-Rate: 52.0 Status: 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;
}
答案 0 :(得分:0)
这取决于您的打印方法。如果您的Room类与下面的类相同,那么displayrooms()
方法也将打印出PremiumRooms。
public class Room {
private String roomId;
private String description;
private double dailyRate;
private char status;
public Room(String roomId, String description, double dailyRate) {
this.roomId = roomId;
this.description = description;
this.dailyRate = dailyRate;
this.status = 'A';
}
public void print() {
StringBuilder str = new StringBuilder();
str.append("Room ID: ").append(roomId).append("\n")
.append("Description: ").append(description).append("\n")
.append("Daily-Rate: ").append(dailyRate).append("\n")
.append("Status: ").append(status);
System.out.println(str.toString());
}
}
但是,它会省略Premium Room类中的任何其他字段。我还要包括你需要覆盖PremiumRoom Class中的print方法,如
public class PremiumRoom extends Room {
private int freeNights;
private double discountRate;
private double nextBookingDiscountVoucher;
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;
}
public void print() {
super.print();
StringBuilder str = new StringBuilder();
str.append("Free Nights: ").append(freeNights).append("\n")
.append("Discount Rate: ").append(discountRate).append("\n")
.append("Voucher: ").append(nextBookingDiscountVoucher);
System.out.println(str.toString());
}
}
并调用
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();
}
public static void main(String[] args) {
displayrooms();
}
}