zoukankan      html  css  js  c++  java
  • 3里氏代换原则LSP

    一、什么是里氏代换原则

      里氏代换原则(Liskov Substitution Principle): 一个软件实体如果使用的是一个父类的话,那 么一定适用于其子类,而且它察觉不出父类和子 类对象的区别。也就是说,在软件里面,把父类 替换成它的子类,程序的行为没有变化。

    二、反过来的代换不成立

      里氏代换原则(Liskov Substitution Principle): 一个软件实体如果使用的是一个子类的话,那 么它不能适用于其父类。

    三、企鹅是鸟类吗??

    四、正方形是一种长方形吗??

    正方形不是长方形的子类,因此之间不存在里氏代换关系。

    五、好骗的Java编译器

    六、原来还有一个四边形的概念?

    能够骗过Java编译器,真的符合里氏代换原则吗?答案是否定的

    1 public class Person {
    2     public void display() {
    3         System.out.println("this is person");
    4     }
    5 }

    男人

    1 public class Man extends Person {
    2 
    3     public void display() {
    4         System.out.println("this is man");
    5     }
    6 }

    测试

     1 public class MainClass {
     2     public static void main(String[] args) {
     3         Person person = new Person();
     4 //        display(person);
     5         
     6         Man man = new Man();
     7         display(man);
     8     }
     9     
    10     public static void display(Man man) {
    11         man.display();
    12     }
    13 }

    =================================================================

    ex2:

    鸟  接口

    1 //
    2 public interface Bird {
    3     public void fly();
    4 }

    老鹰

    1 //老鹰
    2 public class Laoying implements Bird {
    3 
    4     public void fly() {
    5         System.out.println("老鹰在飞");
    6     }
    7 }

    麻雀

    1 //麻雀
    2 public class Maque implements Bird {
    3 
    4     public void fly() {
    5         System.out.println("麻雀在飞");
    6     }
    7 }

    测试

    1 public class MainClass {
    2     public static void main(String[] args) {
    3         fly(new Laoying());
    4     }
    5     
    6     public static void fly(Bird bird) {
    7         bird.fly();
    8     }
    9 }

    =============================================================

    ex3:

    四边形  接口

    1 //四边形
    2 public interface Sibianxing {
    3     public long getWidth();
    4     public long getHeight();
    5 }

    长方形

     1 //长方形
     2 public class ChangFX implements Sibianxing{
     3     private long width;
     4     private long height;
     5     
     6     public long getWidth() {
     7         return width;
     8     }
     9     public void setWidth(long width) {
    10         this.width = width;
    11     }
    12     public long getHeight() {
    13         return height;
    14     }
    15     public void setHeight(long height) {
    16         this.height = height;
    17     }
    18 }

    正方形

     1 //正方形
     2 public class ZhengFX implements Sibianxing{
     3     private long side;
     4 
     5     public long getHeight() {
     6         return this.getSide();
     7     }
     8 
     9     public long getWidth() {
    10         return this.getSide();
    11     }
    12 
    13     public void setHeight(long height) {
    14         this.setSide(height);
    15     }
    16 
    17     public void setWidth(long width) {
    18         this.setSide(width);
    19     }
    20 
    21     public long getSide() {
    22         return side;
    23     }
    24 
    25     public void setSide(long side) {
    26         this.side = side;
    27     }
    28 }

    测试

     1 public class MainClass {
     2     public static void main(String[] args) {
     3         ChangFX changfx = new ChangFX();
     4         changfx.setHeight(10);
     5         changfx.setWidth(20);
     6         test(changfx);
     7         
     8         ZhengFX zhengfx = new ZhengFX();
     9         zhengfx.setHeight(10);
    10         test(zhengfx);
    11     }
    12     
    13     public static void test(Sibianxing sibianxing) {
    14         System.out.println(sibianxing.getHeight());
    15         System.out.println(sibianxing.getWidth());
    16     }
    17     
    18 //    public static void resize(Sibianxing sibianxing) {
    19 //        while(sibianxing.getHeight() <= sibianxing.getWidth()) {
    20 //            sibianxing.setHeight(sibianxing.getHeight() + 1);
    21 //            test(sibianxing);
    22 //        }
    23 //    }
    24 }
  • 相关阅读:
    ModelBinder——ASP.NET MVC Model绑定的核心
    asp.net
    深入C#内存管理来分析 值类型、引用类型、装箱、拆箱、堆栈几个概念
    C#网络编程
    [Architecture Pattern] Service Locator
    RestSharp使用详解
    系统性能优化一例
    C#开发的高性能EXCEL导入、导出工具DataPie
    ASP.NET MVC以ValueProvider为核心的值提供系统: DictionaryValueProvider
    ASP.NET MVC以ValueProvider为核心的值提供系统: NameValueCollectionValueProvider
  • 原文地址:https://www.cnblogs.com/justdoitba/p/9036559.html
Copyright © 2011-2022 走看看