.NET基礎回顧(三)

來源:互聯網
上載者:User

標籤:style   blog   color   ar   strong   sp   div   on   log   

一. 裡氏替換原則:LSP

定義:子類可以替換父類的位置,並且程式的功能不受影響(父類變數指向子類對象)。因為父類有的功能子類都有,所以不影響程式的功能。

程式樣本:

1 Person p = new Person();2 p.SayHi();//調用父類的3 Student s = new Student();4 s.SayHi(); //調用子類的5 //裡氏替換原則6 Person p1 = new Student();7 P1.SayHi(); //調用父類的

二. 類型轉換

1. bool b = p is Person;判斷變數所指的類的類型。

2. as轉換

         Person p = new Person();

         Student s1 = (Student)p;//可能會報異常

         //如果轉換成功,引用指向s2,如果轉換失敗,返回null

         Student s2 = p as Student;

三.虛方法、多態

1. 虛方法:允許子類覆蓋父類的方法。

程式樣本:

 1 //Person.cs 2 class Person 3     { 4         public virtual void SayHi() 5         { 6             Console.WriteLine("我是人。"); 7         } 8 } 9 10 //Chinese.cs11 class Chinese:Person12     {13         public override void SayHi()14         {15             Console.WriteLine("我是中國人。");16         }17 }18 19 //GuangDongRen.cs20 class GuangDongRen:Chinese21     {22         public override void SayHi()23         {24             Console.WriteLine("我是廣東人。");25         }26     }27 28 //Main29 static void Main(string[] args)30         {31             Person p = new GuangDongRen();32             p.SayHi();33 //輸出“廣東人”34             //如果GuangDongRen類裡面沒有重寫SayHi()方法,則輸出“中國人”35 36             Console.ReadKey();37         }

2. 多態,同種行為,對於不同事物,有不同的表現。

程式樣本:

 

 1 //Person.cs 2 public virtual void WashBody() 3         { 4             Console.WriteLine("人在洗澡"); 5         } 6  7 //Boy.cs 8 class Boy:Person 9     {10         public override void WashBody()11         {12             Console.WriteLine("男孩在洗澡");13         }14     }15 16 //Girl.cs17 class Girl:Person18     {19         public override void WashBody()20         {21             Console.WriteLine("女孩在洗澡");22         }23 }24 25 //WashMachine.cs26 class WashMachine27     {28         public void Wash(Person p)29         {30             Console.WriteLine("開始洗澡");31             p.WashBody();32             Console.WriteLine("洗澡結束");33         }34     }35 36 //Main37 static void Main(string[] args)38         {39             Boy p1 = new Boy();40             Girl p2 = new Girl();41             WashMachine wm = new WashMachine();42             wm.Wash(p1);//男孩洗澡        43             wm.Wash(p2);//女孩洗澡44             Console.ReadKey();

3. 將父類類型作為方法的傳回值。(原理是什嗎?)

程式樣本:

 1 //Pet.cs 2 class Pet 3 { 4     public virtual void Shout() 5     { 6         Console.WriteLine("寵物叫"); 7     } 8 } 9 10 //Cat.cs11 class Cat:Pet12 {13     public override void Shout()14     {15         Console.WriteLine("貓咪叫");16     }17 }18 19 //Dog.cs20 class Dog:Pet21 {22     public override void Shout()23     {24         Console.WriteLine("狗狗叫");25     }26 }27 28 //Shop.cs29 class Shop30 {31     public Pet SellPet(string type)32     {33         switch(type)34         {35             case "dog":36                 return new Dog();37             case "cat"38                 return new Cat();39             default:40                 return null;41         }42     }43 }44 45 //Main46 static void Main(string[] args)47 {48     Shop shop = new Shop();49     Pet pet = shop.SellPet("dog");50     pet.Shout();//輸出"狗狗叫"51 }

 

.NET基礎回顧(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.