【问题标题】:Java Classes InheritanceJava 类继承
【发布时间】:2015-11-16 12:48:48
【问题描述】:

在非常棘手的情况下我需要一些帮助。我的程序运行良好,但我被告知要更改课程的运行方式。我所拥有的是一个预订类,它扩展了一个客户类以获取客户详细信息。现在有人告诉我 Booking 类必须具有 Customer 类型的属性,而不是扩展类。然后在我的程序中,我将 Customer 作为一个整体对象加载,而不是逐行读取并将它们插入到文本框中。谁能帮我解决这个问题?

public class Booking implements Serializable{
    
    private String flighttime;
    private String flightlocation;
    private String flightfee;
    private boolean car;
    private boolean insurance;
    private Customer customer;

    /**
     * @return the flighttime
     */
    public String getFlighttime() {
        return flighttime;
    }

    /**
     * @param flighttime the flighttime to set
     */
    public void setFlighttime(String flighttime) {
        this.flighttime = flighttime;
    }

    /**
     * @return the flightlocation
     */
    public String getFlightlocation() {
        return flightlocation;
    }

    /**
     * @param flightlocation the flightlocation to set
     */
    public void setFlightlocation(String flightlocation) {
        this.flightlocation = flightlocation;
    }

    /**
     * @return the flightfee
     */
    public String getFlightfee() {
        return flightfee;
    }

    /**
     * @param flightfee the flightfee to set
     */
    public void setFlightfee(String flightfee) {
        this.flightfee = flightfee;
    }

    /**
     * @return the car
     */
    public boolean getCar() {
        return isCar();
    }

    /**
     * @param car the car to set
     */
    public void setCar(boolean car) {
        this.car = car;
    }

    /**
     * @return the car
     */
    public boolean isCar() {
        return car;
    }

    /**
     * @return the insurance
     */
    public boolean isInsurance() {
        return insurance;
    }

    /**
     * @param insurance the insurance to set
     */
    public void setInsurance(boolean insurance) {
        this.insurance = insurance;
    }
    
    public boolean getInsurance() {
        return isInsurance();
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
}

【问题讨论】:

  • "What I have is a Booking Class which extends a Customer Class to get the Customer details." -- 你不应该这样做,因为 Booking 不是 Customer 类型,即它不满足“is-a”关系。而是预订“拥有”客户。
  • 是的...有道理..仍然对如何做到这一点感到困惑
  • 你的老师是对的。预订是数据的组合——一个客户、一个航班或一堆航班等......,预订不是一个人,不是一个你可以像客户一样交谈或握手的人,如果以后有人进行预订并希望能够更改所涉及的客户,他们应该能够这样做。让 Booking 扩展 Customer 不会让它访问“客户详细信息”,而是试图将类硬塞到它不是的东西中。

标签: java class inheritance types properties


【解决方案1】:

然后在我的程序中,我将 Customer 作为一个整体对象加载,而不是逐行读取并将它们插入到文本框中。

  1. 更改 Booking,使其不会扩展 Customer,因为它不满足“is-a”关系。
  2. 您仍需要像以前一样获取客户数据并创建客户对象,
  3. 但是一旦创建,您也许可以将您的客户放入客户集合中,例如 ArrayList,
  4. 甚至将此集合存储在数据库或文件中,
  5. 如果您需要从众多客户中选择一个客户,那么所有客户可能会在您的 GUI 中显示为 JComboBox。
  6. 既然 Booking 没有扩展 Customer,您可以像任何其他类型的字段一样将 Customer 实例添加到您的 Booking 对象中:通过构造函数参数 public Booking(Customer customer, ...<other params>) 或通过 setter 方法 public void setCustomer(Customer customer) {...},或两者都有。

【讨论】:

  • 好的,所以在预订类中我删除了“扩展客户”。然后我在预订类中创建了一个私人客户属性。但是现在我将如何在创建预订时获取客户数据,因为我删除了“扩展”部分?
  • 是的,我封装了新的客户字段,并在 Booking 类中为客户获取了 GET 和 SET。仍然对如何将客户详细信息与 Booking 类链接感到困惑,但是……我还想保留文件选择器,而不是将我的客户加载到 JCombobox 中。谢谢
  • 关于如何在我的 Booking 类中创建构造函数的任何想法?你是唯一一个明白我需要什么的人。谢谢@Hovercraft 满满的鳗鱼
  • @Luke_i:与任何其他构造函数一样:传入设置预订状态所需的参数,并使用参数设置字段。
  • 如您所见,我是个菜鸟……我是否仍然使用封装字段来获取和设置?谢谢。上面更新了代码以供进一步参考@Hovercraft Full Of Eels
猜你喜欢
  • 2012-02-25
  • 2015-03-14
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
  • 2015-12-10
相关资源
最近更新 更多