C# 多態(通俗理解)

來源:互聯網
上載者:User

標籤:

這裡隨便說說多態,看了上面繼承再看看這篇。

多態:通過繼承實現的不同對象調用相同的方法,表現出不同的行為。

之前的前台調用代碼如下: 

 1 namespace ConsoleApplication7 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             Dog dog = new Dog(); 8             dog.Name = "骨頭"; 9             dog.Show();10             //輸出結果// 小狗喜歡吃:骨頭11 12 13             Cat cat = new Cat();14             cat.Name = "魚";15             cat.Show();16             //輸出結果// 小貓喜歡吃:魚17 18             Mouse mouse = new Mouse();19             mouse.Name ="大米";20             mouse.Show();21             //輸出結果// 老鼠喜歡吃:魚22 23             Panda panda = new Panda();24             panda.Name = "竹子";25             panda.Show();26             //輸出結果// 熊貓喜歡吃:魚27 28             Console.ReadKey();29 30         }31     }32 }

如你所見,每個動物類都是一個對象

狗的對象:dog

貓的對象:cat

老鼠的對象:mouse

熊貓的對象:panda

要讓這些動物實現輸出show()方法,你就要聲明每一個對象,而上面說了,多態是:不同對象調用相同的方法,表現出不同的行為。

而且各種動物類都繼承了Animal類,於是代碼變成這樣:

 1 namespace ConsoleApplication7 2 { 3     public class Animal 4     { 5         protected string name; 6         public string Name 7         { 8             get { return name; } 9             set { name = "新鮮的" + value; }10         }11         public virtual void Show()12         {13             Console.WriteLine("我是各種動物的父類");14         }15     }16 17     public class Cat : Animal18     {19         public override void Show()20         {21             Console.WriteLine("小貓喜歡吃:" + name);22         }23     }24 25     public class Dog : Animal26     {27         public override void Show()28         {29             Console.WriteLine("小貓喜歡吃:" + name);30         }31     }32 33     public class Mouse : Animal34     {35         public override void Show()36         {37             Console.WriteLine("老鼠喜歡吃:" + name);38         }39     }40 41     public class Panda : Animal42     {43         public override void Show()44         {45             Console.WriteLine("熊貓喜歡吃:" + name);46         }47     }48 49 }

前台代碼:

 1 namespace ConsoleApplication7 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7  8             Animal animal; 9             animal = new Dog();10             animal.Name = "骨頭";11             animal.Show();12 13             animal = new Cat();14             animal.Name = "魚";15             animal.Show();16 17             animal = new Mouse();18             animal.Name = "大米";19             animal.Show();20 21             animal = new Panda();22             animal.Name = "熊貓";23             animal.Show();24 25             Console.ReadKey();26 27         }28     }29 }

結果:

小狗喜歡吃:新鮮的骨頭

小貓喜歡吃:新鮮的魚

老鼠喜歡吃:新鮮的大米

熊貓喜歡吃:新鮮的竹子

animal對象被賦值不同的執行個體,但是animal只調用Show()方法就得到不同的結果。

C# 多態(通俗理解)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.