Java类继承

标签 java class inheritance types properties

在相当棘手的情况下我需要一些帮助。我的程序运行良好,但我被告知要改变类(class)的工作方式。我拥有的是一个预订类别,它扩展了客户类别以获取客户详细信息。现在我被告知 Booking Class 必须具有 Customer 类型的属性,而不是扩展 Class。然后在我的程序中,我将 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;
    }
    
}

最佳答案

Then in my program I load the Customer as a whole object instead of reading line by line and inserting them into the text boxes.

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

关于Java类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32159553/

相关文章:

java - android类构造函数中的最佳实践

c++ - 在共享库的类中调用 GSL 函数

java - 删除具有特定文本值的子节点的 XML 节点

java - 我如何急切地为每个租户初始化 bean?

c++ - 类模板的外部成员函数

performance - Solidity 中的组合优于继承 - Gas 效率

java - 如何测试一个类及其子类

java - 有效地在两倍大的字符串中查找字符串

java - spring boot中外键引用主键

python - 我没有正确使用setter方法吗?