为什么我的方法不打印子类中的对象?

Why is my method not printing objects from a subclass?

这是我的数组:

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 对象。我该如何解决这个问题? premiumroom 是 Room 的子类。

这是它的输出,缺少 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

有些人要求提供 RoomPremiumRoom 代码。他们在这里:

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;
    }

这取决于您的打印方式。如果您的 class 房间与下面的房间类似,那么 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 class 中的任何其他字段。我也订购了它,您需要重写 PremiumRoom Class 中的打印方法,例如

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();
    }
}