我想在不使用 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/