1 课程讲解
1.1 开闭原则定义
1.2 不重要内容
2 代码coding
2.1 基类
2.2 需求:打印出原价和折扣后的价格。(接口不应该随意变化,面向接口编程)
1 课程讲解
1.1 开闭原则定义
一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。
1.2 不重要内容
(抽象构建框架,实现扩展细节)
2 代码coding
2.1 基类
测试类:
package com.geely.design.principle.openclose; public class TestJavaCourse { public static void main(String[] args) { Icourse icourse = new JavaCourse(96,"java开发教程",298.00); System.out.println("购买课程编号为:"+icourse.getCourseId()+";课程名称为:"+icourse.getCourseName()+";课程价格为:"+icourse.getCourcePrice()); } }
实体类:
package com.geely.design.principle.openclose; public class JavaCourse implements Icourse { private Integer courseId; private String courseName; private Double coursePrice; public JavaCourse(Integer courseId, String courseName, Double coursePrice) { this.courseId = courseId; this.courseName = courseName; this.coursePrice = coursePrice; } @Override public Integer getCourseId() { return this.courseId; } @Override public String getCourseName() { return this.courseName; } @Override public Double getCourcePrice() { return this.coursePrice; } }
接口:
package com.geely.design.principle.openclose; public interface Icourse { Integer getCourseId();//获取课程ID String getCourseName();//获取课程名称 Double getCourcePrice();//获取课程价格 }
打印日志:
购买课程编号为:96;课程名称为:java开发教程;课程价格为:298.0
Process finished with exit code 0
2.2 需求:打印出原价和折扣后的价格。(接口不应该随意变化,面向接口编程)
测试类:
package com.geely.design.principle.openclose; public class TestJavaCourse { public static void main(String[] args) { TestJavaCourse testJavaCourse = new TestJavaCourse(); testJavaCourse.showDiscountPrice(); } public void showDiscountPrice() { Icourse icourse = new JavaDiscountCourse(96,"java开发教程",298.00); JavaDiscountCourse javaCourse = (JavaDiscountCourse)icourse; System.out.println("购买课程编号为:"+icourse.getCourseId()+";课程名称为:"+icourse.getCourseName()+";课程价格为:"+javaCourse.getOrignalPrice()+"折后价格为:"+javaCourse.getDisCountPrice()); } public void showOrignalPrice() { Icourse icourse = new JavaCourse(96,"java开发教程",298.00); System.out.println("购买课程编号为:"+icourse.getCourseId()+";课程名称为:"+icourse.getCourseName()+";课程价格为:"+icourse.getCourcePrice()); } }
实体类:
package com.geely.design.principle.openclose; public class JavaCourse implements Icourse { private Integer courseId; private String courseName; private Double coursePrice; public JavaCourse(Integer courseId, String courseName, Double coursePrice) { this.courseId = courseId; this.courseName = courseName; this.coursePrice = coursePrice; } @Override public Integer getCourseId() { return this.courseId; } @Override public String getCourseName() { return this.courseName; } @Override public Double getCourcePrice() { return this.coursePrice; } }
补充类:
package com.geely.design.principle.openclose; public class JavaDiscountCourse extends JavaCourse{ private Double coursePrice; public JavaDiscountCourse(Integer courseId, String courseName, Double coursePrice) { super(courseId, courseName, coursePrice); } public Double getDisCountPrice() { return super.getCourcePrice()*0.8; } public Double getOrignalPrice() { return super.getCourcePrice(); } }
接口:
package com.geely.design.principle.openclose; public interface Icourse { Integer getCourseId();//获取课程ID String getCourseName();//获取课程名称 Double getCourcePrice();//获取课程价格 }
测试结果:
购买课程编号为:96;课程名称为:java开发教程;课程价格为:298.0折后价格为:238.4
Process finished with exit code 0
诸葛