3里氏代換原則LSP

1、什麼是里氏代換原則ide

  里氏代換原則(Liskov Substitution Principle): 一個軟件實體若是使用的是一個父類的話,那 麼必定適用於其子類,並且它察覺不出父類和子 類對象的區別。也就是說,在軟件裏面,把父類 替換成它的子類,程序的行爲沒有變化。測試

 

2、反過來的代換不成立this

  里氏代換原則(Liskov Substitution Principle): 一個軟件實體若是使用的是一個子類的話,那 麼它不能適用於其父類。spa

 

3、企鵝是鳥類嗎??3d

 

4、正方形是一種長方形嗎??code

正方形不是長方形的子類,所以之間不存在里氏代換關係。對象

 

5、好騙的Java編譯器blog

 

6、原來還有一個四邊形的概念?接口

可以騙過Java編譯器,真的符合里氏代換原則嗎?答案是否認的ip

 

 

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 }
相關文章
相關標籤/搜索