标签:port length com message div uml类图 变更 des 技术
1.定义:如果对每一个类型为T1的对象o1,都有类型为T2的对象o2,
使得以T1定义的所有程序P在所有的对象o1都替换成o2时,
程序P的行为没有发生变化,那么类型T2是类型T1的子类型。
2.定义扩展:一个软件实体如果适用一个父类的话,那一定适用于其子类,
所有引用父类的地方必须能透明地使用其子类,
子类对象能够替换父类对象,而程序逻辑不变。
3.引申意义:子类可以扩展父类的功能,但不能改变父类原有的功能。
含义1:子类可以实现父类的抽象方法,但不能覆盖父类的非抽象方法。
含义2:子类中可以增加自己特有的方法。
含义3:但子类的方法重载父类的方法时,方法的前置条件(即方法的输入/入参)要比父类方法的输入参数更宽松。
含义4:但子类的方法实现父类的方法时(重写/重载或实现抽象方法),方法的后置条件(即方法的输出/返回值)要比父类方法的输出参数更严格或相等。
4.优点1:约束继承泛滥,开闭原则的一种体现。
优点2:加强程序的健壮性,同时变更时也可以做到非常好的兼容性提高程序的维护性、扩展性。降低需求变更时引入的风险。
5.实例目录package
6.实例UML类图
7.代码
1 package com.geely.design.principle.liskovsubstitution; 2 3 public interface Quadrangle { 4 long getWidth(); 5 long getLength(); 6 }
1 package com.geely.design.principle.liskovsubstitution; 2 3 public class Rectangle implements Quadrangle{ 4 private long length; 5 private long width; 6 7 public long getWidth() { 8 return width; 9 } 10 11 public long getLength() { 12 return length; 13 } 14 public void setLength(long length){ 15 this.length = length; 16 } 17 public void setWidth(long width){ 18 this.width = width; 19 } 20 /*public void setLength(long length){ 21 this.length = length; 22 } 23 public void setWidth(long width){ 24 this.width = width; 25 } 26 public long getLength(){ 27 return length; 28 } 29 public long getWidth(){ 30 return width; 31 }*/ 32 }
1 package com.geely.design.principle.liskovsubstitution; 2 3 public class Square implements Quadrangle{// extends Rectangle 4 private long sideLength; 5 6 public void setSideLength(long sideLength){ 7 this.sideLength = sideLength; 8 } 9 public long getSideLength(){ 10 return sideLength; 11 } 12 13 public long getWidth() { 14 return sideLength; 15 } 16 17 public long getLength() { 18 return sideLength; 19 } 20 21 /*public void setSideLength(long sideLength){ 22 this.sideLength = sideLength; 23 } 24 public long getSideLength(){ 25 return sideLength; 26 } 27 28 @Override 29 public void setLength(long length) { 30 setSideLength(length); 31 } 32 33 @Override 34 public void setWidth(long width) { 35 setSideLength(width); 36 } 37 38 @Override 39 public long getLength() { 40 return getSideLength(); 41 } 42 43 @Override 44 public long getWidth() { 45 return getSideLength(); 46 }*/ 47 }
1 package com.geely.design.principle.liskovsubstitution; 2 3 public class Test { 4 /*public static void resize(Rectangle rectangle){ 5 while(rectangle.getWidth() <= rectangle.getLength()){ 6 rectangle.setWidth(rectangle.getWidth() + 1); 7 System.out.println("width: " + rectangle.getWidth() + ",length: " + rectangle.getLength()); 8 } 9 System.out.println("resize方法结束 width: " + rectangle.getWidth() + ",length: " + rectangle.getLength() ); 10 }*/ 11 public static void resize(Quadrangle quadrangle){ 12 while(quadrangle.getWidth() <= quadrangle.getLength()){ 13 quadrangle.setWidth(quadrangle.getWidth() + 1); 14 System.out.println("width: " + quadrangle.getWidth() + ",length: " + quadrangle.getLength()); 15 } 16 System.out.println("resize方法结束 width: " + quadrangle.getWidth() + ",length: " + quadrangle.getLength() ); 17 } 18 /*public static void main(String[] args){ 19 Rectangle rectangle = new Rectangle(); 20 rectangle.setWidth(10); 21 rectangle.setLength(20); 22 resize(rectangle); 23 }*/ 24 25 public static void main(String[] args){ 26 Square square = new Square(); 27 square.setSideLength(10); 28 resize(square); 29 } 30 }
1 package com.geely.design.principle.liskovsubstitution.methodinput; 2 3 import java.util.HashMap; 4 5 public class Base { 6 public void method(HashMap<String,Object> map){// 7 System.out.println("父类被执行"); 8 } 9 10 }
1 package com.geely.design.principle.liskovsubstitution.methodinput; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class Child extends Base{ 7 @Override 8 public void method(HashMap<String,Object> map) { 9 System.out.println("子类HashMap入参方法被执行"); 10 } 11 12 public void method(Map<String, Object> map) { 13 System.out.println("子类Map入参方法被执行"); 14 } 15 }
1 package com.geely.design.principle.liskovsubstitution.methodinput; 2 3 import java.util.HashMap; 4 5 public class Test { 6 public static void main(String[] args) { 7 Base child = new Child(); 8 HashMap<String,Object> hashMap = new HashMap<String,Object>(); 9 child.method(hashMap); 10 //TreeMap<String,Object> treeMap = new TreeMap<String,Object>(); 11 //child.method(treeMap); 12 } 13 }
1 package com.geely.design.principle.liskovsubstitution.methodoutput; 2 3 import java.util.Map; 4 5 public abstract class Base { 6 public abstract Map<String, Object> method(); 7 }
1 package com.geely.design.principle.liskovsubstitution.methodoutput; 2 3 import java.util.HashMap; 4 5 public class Child extends Base{ 6 //@Override 7 public HashMap<String, Object> method() {// 8 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 9 System.out.println("子类method被执行"); 10 hashMap.put("message","子类method被执行"); 11 return hashMap; 12 } 13 }
标签:port length com message div uml类图 变更 des 技术
原文地址:https://www.cnblogs.com/linbq1911/p/10205025.html