【问题标题】:How to connect two different classes如何连接两个不同的类
【发布时间】:2015-08-25 06:54:07
【问题描述】:

我想在不使用扩展的情况下将Customer 类的属性添加到Booking 类,因为CustomerBooking 的属性而不是扩展。如何以最好的方式做到这一点?

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

【问题讨论】:

  • book.getCustomer().getPassportID()

标签: java class inheritance properties


【解决方案1】:

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

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

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

【讨论】:

  • 我需要这个是为了使用 Booking 类,并且仍然可以访问客户属性,例如姓名、姓氏等...更新了上面的代码以供进一步参考。谢谢
  • 因为它是我不能使用 SetPersonName、SetPersonSurname 等。因为它们在客户类中。谢谢@sᴜʀᴇsʜᴀᴛᴛᴀ
  • @Luke_i 你为什么不能?尝试。你可以。
  • 为什么要通过预订调用客户的setter?这听起来像是一种巨大的设计气味。读起来就像您更改了该预订的客户属性,但根据这些对象的来源,您可能会全局更改客户属性。
【解决方案2】:

您可以通过创建一个新的抽象类 Reservable(或其他)来实现您想要的,该类具有由 Customer 类定义的 4 个字段(以及相应的 getter 和 setter),并让两个类(Customer 和 Booking)扩展它。这样一来,预订就不是客户,因为它们不会相互扩展,但它们通过共同的抽象祖先共享一些属性。

另一种方法是在 Booking 类中简单地定义一个 getter 和 setter,以通过 customer 字段访问这些属性。

public String getConsulId() { 
   return customer.getConsulId(); 
}

【讨论】:

    【解决方案3】:

    你可以这样做:-

    public Booking (Customer customer){
           this.customer = customer;
    }
    
    bookingObj.getCustomer().get..();
    bookingObj.getCustomer().set..();
    

    这是一个组合关系

    【讨论】:

      【解决方案4】:

      首先创建Customerobj。

      Customer customer = new Customer();
      customer.setCustomerName("name");
      customer.setCustomerSurname("surname");
      ...and so on
      

      您已经将Customer 作为Booking 类中的成员变量。现在,您可以使用 setter 方法在 Booking 类中设置 Customer 对象。

      customerbooking.setCustomer(customer);
      

      或者使用构造函数

      public Booking (Customer customer){
         this.customer = customer;
      }
      

      之后,您可以像这样访问Booking 类中的任何位置的Customer 类字段:

      getCustomer().getCustomerName();
      

      Booking类之外

      customerbooking.getCustomer().getCustomerName();
      

      【讨论】:

      • 是的,我封装了该字段并获得了 Get 和 Set。但是,如果我只有客户作为属性,我将如何确定我想要存储客户姓名、客户姓氏等数据的位置?谢谢
      • 你能给我一个客户名称的例子吗?无法理解如何实现它。谢谢
      • 好吧,现在说得通了.. 但是我在哪里输入:getCustomer().getCustomerName(); (因为我封装了这些字段,所以我已经有一个 Get Customer)谢谢
      • Booking 类中的任何其他方法,一旦你在其中设置了客户 obj,你想要 Customer 类属性,因为它是成员变量
      • 但我不想创建新客户.. 我只想使用客户属性创建预订。如果您看到我从文本框中读取的上述代码并尝试设置客户属性。但是像这样我不能,因为我无法访问客户属性。谢谢
      【解决方案5】:

      通常你可以创建getter和setter并通过它们获取属性

      示例: bookinginstance.getCustomer().getConsultandId()

      但更好的方法是创建业务方法,它不会破坏实体原则。

      示例: 如果要显示预订创建方法displayCustomerData() 的客户数据

      如果您想向客户收费,请在 Booking 类中创建 charge() 方法。

      等等。

      public class Booking implements Serializable{
          
          private String flighttime;
          private String flightlocation;
          private String flightfee;
          private boolean car;
          private boolean insurance;
          private Customer customer;
          public void displayCustomerData(){
               System.out.println(customer.getName() + " "+ customer.getSurname());
          }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-05
        • 1970-01-01
        • 2023-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多