java - 如何连接两个不同的类

标签 java class inheritance properties

我想在不使用 Extend 的情况下将 Customer 类的属性获取到 Booking 类,因为 Customer预订而不是延期。如何以最好的方式做到这一点?

public class Booking implements Serializable{

    private String flighttime;
    private String flightlocation;
    private String flightfee;
    private boolean car;
    private boolean insurance;
    private Customer customer;

    ...
}


public class Customer extends Person implements Serializable {

    private String passportID;
    private String consultantname;
    private String consultantsurname;
    private String consulid;

    ....
}

private void savebookingButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Booking customerbooking = new Booking();

    try {
        if (custnameTF.getText().equals("")) {
            throw new EmptyField("Please Insert Customer");
        } else {
            FileOutputStream fos = new FileOutputStream(
                                          "Bookings/" + custidTF.getText() + ".txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            customerbooking.setPersonName((custnameTF.getText()));
            customerbooking.setPersonSurname((custsurnameTF.getText()));
            customerbooking.setPersonID((custidTF.getText()));
            customerbooking.setConsultantname(consnameTF.getText());
            customerbooking.setConsultantsurname((conssurnameTF.getText()));
            customerbooking.setConsulid(considTF.getText());
            customerbooking.setFlightlocation(locationCB.getSelectedItem().toString());
            customerbooking.setFlighttime(timeCB.getSelectedItem().toString());
            customerbooking.setFlightfee(feeCB.getSelectedItem().toString());
            customerbooking.setCar(carRB.isSelected());
            customerbooking.setInsurance(insuranceRB.isSelected());
        }
    }
}

最佳答案

您已经通过将 Customer 作为属性(property)来做到这一点。

要使用 Booking 类实例获取或设置 Customer 的属性,您必须编写

bookingInstance.getCustomer.setSomething(....
bookingInstance.getCustomer.getSomething(....

关于java - 如何连接两个不同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197347/

相关文章:

java - 读取用户文件系统返回 AccessDeniedException

entity-framework - 包括 Entity Framework TPH 类的导航属性

python - 一个类从一个不是类型的对象继承是什么意思?

javascript - 为什么需要通过实例访问原型(prototype)?

c++ - 使用此指针时 C++ 析构函数错误的原因?

javascript - 如何更新 Javascript 以返回 1

c# - 泛型和继承 - 方法参数

java - 从 Activity 返回时更改菜单项

java - 将 ImageByteArray 转换为 BufferedImage

Java Spring RestTemplate POST 问题,而 Postman 工作