在相当棘手的情况下我需要一些帮助。我的程序运行良好,但我被告知要改变类(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.
- 更改预订,以便不会扩展客户,因为它不满足“is-a”关系。
- 您仍然需要以与之前相同的方式获取客户数据并创建客户对象,
- 但一旦创建,您也许可以将 Customer 放入 Customer 集合中,例如 ArrayList,
- 甚至将此集合存储在数据库或文件中,
- 如果您需要从多个客户中选择一个客户,那么所有客户可能会在 GUI 中显示为 JComboBox。
- 由于 Booking 不扩展 Customer,因此您可以像任何其他类型的字段一样将 Customer 实例添加到 Booking 对象中:通过构造函数参数
public Booking(Customer customer, ...<other params>)
或通过 setter 方法,public void setCustomer(Customer customer) {...}
,或两者兼而有之。
关于Java类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32159553/