标签:ble bubuko img 维护 print 技术 构建 div sig
1.定义:一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。
2.用抽象构建框架,用实现扩展细节
3.优点:提高软件系统的可复用性及可维护性
4.实例目录package
5.UML图
6.代码
1 package com.geely.design.principle.openclose; 2 3 public interface ICourse { 4 Integer getId(); 5 String getName(); 6 Double getPrice(); 7 //Double getDiscountPrice(); 8 }
1 package com.geely.design.principle.openclose; 2 3 public class JavaCourse implements ICourse { 4 private Integer id; 5 private String name; 6 private Double price; 7 public JavaCourse(){} 8 public JavaCourse(Integer id, String name, Double price){ 9 this.id = id; 10 this.name = name; 11 this.price = price; 12 } 13 14 public Integer getId() { 15 return id; 16 } 17 18 /*public Double getDiscountPrice() { 19 return price * 0.8; 20 }*/ 21 22 public String getName() { 23 return name; 24 } 25 26 public Double getPrice() { 27 return price; 28 } 29 30 @Override 31 public String toString() { 32 return "JavaCourse[id="+id+",name="+name+",price="+price+"]"; 33 } 34 }
1 package com.geely.design.principle.openclose; 2 3 public class JavaDiscountCourse extends JavaCourse { 4 public JavaDiscountCourse(Integer id, String name, Double price){ 5 super(id,name,price); 6 } 7 8 /*public Double getOriginPrice(){ 9 return super.getPrice(); 10 } 11 @Override 12 public Double getPrice() { 13 return super.getPrice() * 0.8; 14 }*/ 15 public Double getDiscountPrice(){ 16 return super.getPrice() * 0.8; 17 } 18 @Override 19 public Double getPrice() { 20 return super.getPrice(); 21 } 22 }
1 package com.geely.design.principle.openclose; 2 3 public class Test { 4 public static void main(String[] args) { 5 ICourse iCourse = new JavaDiscountCourse(96,"java从零到企业级电商开发",348d); 6 System.out.println(iCourse); 7 JavaDiscountCourse javaCourse = (JavaDiscountCourse)iCourse; 8 System.out.println("课程id:" + javaCourse.getId()+",课程名称:"+javaCourse.getName()
+",原价:"+javaCourse.getPrice()+ ",现价课程价格:" + javaCourse.getDiscountPrice()); 9 } 10 }
标签:ble bubuko img 维护 print 技术 构建 div sig
原文地址:https://www.cnblogs.com/linbq1911/p/10204461.html