
public interface IBook
{
string getName();
int getPrise();
string getAuthor();
}小说类:public class NovelBook implements IBook
{
private String name;
private int price;
private String author;
public NovelBook(String _name, int _price, String _author)
{
this.name = _name;
this.price = _price;
this.author = _author;
}
public String getName()
{
return this.author;
}
public int getPrise()
{
return this.price;
}
public String getAuthor()
{
return this.author;
}
}public class BookStore
{
private final static ArrayList<IBook> bookList= new ArrayList<IBook>();
//static 静态模块初始化数据,实际项目中由持久层完成
static{
bookList.add(new NovelBook("天龙八部",3200,"金庸"));
bookList.add(new NovelBook("巴黎圣母院",5600,"雨果"));
}
public static void main(String[] args){
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setMaxmumFractionDigits(2);
System.out.printIn("-----------书店卖出去的书记录如下:--------------");
for(IBook book: bookList){
System.out.printIn("书名:" + book.getName() + "\t作者:" + book.getAuthor() + "\t价格:" + formatter.format(book.)getPrice()/100.0) + "元");
}
}
}
public class OffNovelBook : NovelBook
{
public OffNovelBook(string _name, int _price, string _author)
{
super(_name, _price, _author);
}
@Override
public int getPrice()
{
int selfPrice = super.getPrice();
int offPrice = 0;
if (selfPrice > 4000){
offPrice = selfPrice * 90 / 100;
}
else {
offPrice = selfPrice * 80 / 100;
}
return offPrice;
}
}修改书店打折销售类:static{
bookList.add(new OffNovelBook("天龙八部",3200,"金庸"));
bookList.add(new OffNovelBook("巴黎圣母院",5600,"雨果"));
}仅仅修改上述代码。设计模式学习之——六大设计原则之六:开闭原则,布布扣,bubuko.com
原文:http://blog.csdn.net/fu222cs98/article/details/21495565