C# 物件導向——多態

來源:互聯網
上載者:User

標籤:

多態分三種:1.虛方法  

                 2.抽象類別

                 3.介面

1、虛方法
1、將父類的方法標記為虛方法 ,使用關鍵字 virtual,這個函數可以被子類重新寫一個遍。

如:

class Program    {        static void Main(string[] args)        {            Chinese cn1 = new Chinese("韓梅梅");            Chinese cn2 = new Chinese("李雷");            American a1 = new American("科比布萊恩特");            American a2 = new American("奧尼爾");            Person[] pers = { cn1, cn2,  a1, a2, };            for (int i = 0; i < pers.Length; i++)            {                pers[i].SayHello();            }            Console.ReadKey();        }    }    /// <summary>    /// 父類——人類    /// </summary>    public class Person    {        private string _name;        public string Name        {            get { return _name; }            set { _name = value; }        }        public Person(string name)        {            this.Name = name;        }        public virtual void SayHello()        {            Console.WriteLine("我是人類");        }    }    /// <summary>    /// 子類——中國人    /// </summary>    public class Chinese : Person    {        public Chinese(string name)            : base(name)        {        }        public override void SayHello()        {            Console.WriteLine("我是中國人,我叫{0}", this.Name);        }    }      /// <summary>    /// 子類——美國人    /// </summary>    public class American : Person    {        public American(string name)            : base(name)        {        }        public override void SayHello()        {            Console.WriteLine("我叫{0},我是米國人", this.Name);        }    }
View Code

2、抽象類別
當父類中的方法不知道如何去實現的時候,可以考慮將父類寫成抽象類別,將方法寫成抽象方法。

class Program    {        static void Main(string[] args)        {            //狗狗會叫 貓咪會叫            Animal a = new Cat();//new Dog();            a.Bark();                        Console.ReadKey();        }    }    /// <summary>    /// 父類——動物    /// </summary>    public abstract class Animal    {        public virtual void T()        {            Console.WriteLine("動物有聲明");        }        private int _age;        public int Age        {            get { return _age; }            set { _age = value; }        }        public Animal(int age)        {            this.Age = age;        }        public abstract void Bark();        public abstract string Name        {            get;            set;        }        public Animal()        {                     }    }    /// <summary>    /// 子類——測試    /// </summary>    public abstract class Test : Animal    {             }    /// <summary>    ///子類——狗    /// </summary>    public class Dog : Animal    {        public override void Bark()        {            Console.WriteLine("狗狗WangWang的叫");        }        public override string Name        {            get            {                throw new NotImplementedException();            }            set            {                throw new NotImplementedException();            }        }    }    /// <summary>    /// 子類——貓    /// </summary>    public class Cat : Animal    {        public override void Bark()        {            Console.WriteLine("貓咪喵喵的叫");        }        public override string Name        {            get            {                throw new NotImplementedException();            }            set            {                throw new NotImplementedException();            }        }    }
View Code

抽象類別特點:

1).抽象成員必須標記為abstract,並且不能有任何實現。

public abstract class Animal
{

  public abstract void Bark();

}

2).抽象成員必須在抽象類別中。
3).抽象類別不能被執行個體化

4).子類繼承抽象類別後,必須把父類中的所有抽象成員都重寫。

(除非子類也是一個抽象類別,則可以不重寫)
5).抽象成員的存取修飾詞不能是private
6).在抽象類別中可以包含執行個體成員。
並且抽象類別的執行個體成員可以不被子類實現

7).抽象類別是有建構函式的。雖然不能被執行個體化。


8)、如果父類的抽象方法中有參數,那麼。繼承這個抽象父類的子類在重寫父類的方法的時候必須傳入對應的參數。

如果抽象父類的抽象方法中有傳回值,那麼子類在重寫這個抽象方法的時候 也必須要傳入傳回值。

3.介面

class Program    {        static void Main(string[] args)        {            IFlyable fly = new Bird();//new Person();            fly.Fly();            Console.ReadKey();        }    }    public class Person:IFlyable    {        public void Fly()        {            Console.WriteLine("人類在飛");        }    }    public class Student     {        public void Fly()        {            Console.WriteLine("人類在飛");        }    }    public class Bird : IFlyable    {        public void Fly()        {            Console.WriteLine("鳥在飛");        }    }    public interface IFlyable    {        //不允許有存取修飾詞 預設為public        //方法、自動屬性        void Fly();    }    public interface M1    {        void Test1();    }    public interface M2    {        void Test2();    }    public interface M3    {        void Test3();    }    public interface SupperInterface : M1, M2, M3    {             }    public class Car : SupperInterface    {        public void Test1()        {            throw new NotImplementedException();        }        public void Test2()        {            throw new NotImplementedException();        }        public void Test3()        {            throw new NotImplementedException();        }    }
View Code

介面是一種規範。
只要一個類繼承了一個介面,這個類就必須實現這個介面中所有的成員

為了多態。介面不能被執行個體化。
也就是說,介面不能new(不能建立對象)

介面中的成員不能加“存取修飾詞”,介面中的成員存取修飾詞為public,不能修改。


(預設為public)介面中的成員不能有任何實現(“光說不做”,只是定義了一組未實現的成員)。


介面中只能有方法、屬性、索引器、事件,不能有“欄位”和建構函式。

介面與介面之間可以繼承,並且可以多繼承。

介面並不能去繼承一個類,而類可以繼承介面 (介面只能繼承於介面,而類既可以繼承介面,也可以繼承類)

實現介面的子類必須實現該介面的全部成員。

一個類可以同時繼承一個類並實現多個介面,如果一個子類同時繼承了父類A,並實現了介面IA,那麼文法上A必須寫在IA的前面。


class MyClass:A,IA{},因為類是單繼承的。


顯示實現介面的目的:解決方案的重名問題
什麼時候顯示的去實現介面:
當繼承的借口中的方法和參數一摸一樣的時候,要是用顯示的實現介面


當一個抽象類別實現介面的時候,需要子類去實現介面。


======
如果父類中的方法有預設的實現,並且父類需要被執行個體化,這時可以考慮將父類定義成一個普通類,用虛方法來實現多態。

如果父類中的方法沒有預設實現,父類也不需要被執行個體化,則可以將該類定義為抽象類別。

 

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.