我該如何重構此代碼解決這個OOPS設計問題?Java組合設計問題
public class Book {
private String author;
private double price;
public setPrice(double price){
this.price = price;
}
public getPrice(){
return this.price;
}
}
public class DVD {
private String title;
private double price;
public setPrice(double price){
this.price = price;
}
public getPrice(){
return this.price;
}
}
public class BookAndDvd{
private Book book;
private Dvd dvd;
private double price;
public setBook(Book book){
this.book = book;
}
public setDvd(Dvd dvd){
this.dvd = dvd;
}
}
我想是重構,以便客戶端調用未啓用或getBook().setPrice()
getDvd().setPrice()
?因爲BookAndDvd
類相結合的項目和單個組件不應該有一個價格
這是功課嗎?如果是這樣,請標記爲這樣。 – javamonkey79 2012-01-27 20:46:59
這將是很難爲客戶端調用'getBook()。setPrice()'或'getDvd()。setPrice()''時getBook()'和'getDvd()'不存在。 – 2012-01-27 20:51:29
@ javamonkey79不是家庭作業問題這是一個面試問題 – 2012-01-27 23:16:55