【发布时间】:2015-11-16 12:48:48
【问题描述】:
在非常棘手的情况下我需要一些帮助。我的程序运行良好,但我被告知要更改课程的运行方式。我所拥有的是一个预订类,它扩展了一个客户类以获取客户详细信息。现在有人告诉我 Booking 类必须具有 Customer 类型的属性,而不是扩展类。然后在我的程序中,我将 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;
}
}
【问题讨论】:
-
"What I have is a Booking Class which extends a Customer Class to get the Customer details."-- 你不应该这样做,因为 Booking 不是 Customer 类型,即它不满足“is-a”关系。而是预订“拥有”客户。 -
是的...有道理..仍然对如何做到这一点感到困惑
-
你的老师是对的。预订是数据的组合——一个客户、一个航班或一堆航班等......,预订不是一个人,不是一个你可以像客户一样交谈或握手的人,如果以后有人进行预订并希望能够更改所涉及的客户,他们应该能够这样做。让 Booking 扩展 Customer 不会让它访问“客户详细信息”,而是试图将类硬塞到它不是的东西中。
标签: java class inheritance types properties