1700456328
1700456329
1700456330
1700456331
1700456332
图6-2 扩展后的书店售书类图
1700456333
1700456334
OffNovelBook类继承了NovelBook,并覆写了getPrice方法,不修改原有的代码。新增加的子类OffNovelBook如代码清单6-4所示。
1700456335
1700456336
代码清单6-4 打折销售的小说类
1700456337
1700456338
public class OffNovelBook extends NovelBook{
1700456339
1700456340
public OffNovelBook(String_name,int_price,String_author){
1700456341
1700456342
super(_name,_price,_author);
1700456343
1700456344
}
1700456345
1700456346
//覆写销售价格
1700456347
1700456348
@Override
1700456349
1700456350
public int getPrice(){
1700456351
1700456352
//原价
1700456353
1700456354
int selfPrice=super.getPrice();
1700456355
1700456356
int offPrice=0;
1700456357
1700456358
if(selfPrice>4000){//原价大于40元,则打9折
1700456359
1700456360
offPrice=selfPrice*90/100;
1700456361
1700456362
}else{
1700456363
1700456364
offPrice=selfPrice*80/100;
1700456365
1700456366
}
1700456367
1700456368
return offPrice;
1700456369
1700456370
}
1700456371
1700456372
}
1700456373
1700456374
很简单,仅仅覆写了getPrice方法,通过扩展完成了新增加的业务。书店类BookStore需要依赖子类,代码稍作修改,如代码清单6-5所示。
1700456375
1700456376
代码清单6-5 书店打折销售类
1700456377
[
上一页 ]
[ :1.700456328e+09 ]
[
下一页 ]