抽象方法實現多態___森林裡的動物在快樂的生活

來源:互聯網
上載者:User

標籤:

1.實現頁面效果

2.建立基類或超類(父類)

1 namespace 森林裡的小動物在快樂的生活2 {3    public abstract  class Animal//動物類4     {5        public abstract void Movement();//運動的方法6     }7 }

3.建立衍生類別(魚子類)

namespace 森林裡的小動物在快樂的生活{    public class Fish:Animal //衍生類別派生出基類    {        public override void Movement()//父類的方法        {            Console.WriteLine("魚在水裡遊!");        }    }}

4.建立衍生類別(狗子類)

 1 namespace 森林裡的小動物在快樂的生活 2 { 3     public class Dog:Animal 4     { 5         public override void Movement() 6         { 7             Console.WriteLine("狗在地上跑!"); 8         } 9     }10 }

5.建立衍生類別(鳥子類省略)

6.Main方法

 1 namespace 森林裡的小動物在快樂的生活 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             Console.WriteLine("森林裡的小動物在快樂的生活!"); 8             Console.WriteLine(); 9             Animal father = new Bird();10             Animal father2 = new Dog();11             Animal father3 = new Fish();12             father3.Movement();13             father2.Movement();14             father.Movement();15             Console.ReadLine();16         }17     }18 }

 

抽象方法實現多態___森林裡的動物在快樂的生活

聯繫我們

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