【问题标题】:Why is my method not printing objects from a subclass?为什么我的方法不打印子类中的对象?
【发布时间】: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

你们中的一些人要求提供 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;
    }

【问题讨论】:

  • 分享PremiumRoom类源码?
  • 请显示print方法的代码以及RoomPremiumRoom类的代码
  • (如何)PremiumRoom.print() 是如何实现的?此外,您至少要执行 5x println(),因此您将有 5 个换行符,即两个空行。如果不是,我们会看到错误的代码。
  • 我们没有要求构造函数的代码。我们询问了类的代码,特别是字段定义和print 方法是相关的,以及该代码引用的任何其他内容。
  • 打印方法在哪里?

标签: java arrays object methods subclass


【解决方案1】:

这取决于您的打印方法。如果您的 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());
    }
}

但是,它将省略高级客房类别中的任何其他字段。我也希望包括在内,您需要覆盖 PremiumRoom 类中的 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();
    }
}

【讨论】:

  • 是的!那是我认为的错误。高级房类没有正确编写方法。感谢您的帮助! :)
猜你喜欢
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多