.NET泛型03,泛型型別的轉換,協變和逆變

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   

協變(Convariant)和逆變(Contravariant)的出現,使數組、委託、泛型型別的隱式轉換變得可能。 子類轉換成基類,稱之為協變;基類轉換成子類,稱之為逆變。.NET4.0以來,支援了泛型介面的協變和逆變。

 

  泛型協變

如果子類泛型隱式轉換成基類泛型,使用泛型協變。

 

有這樣的2個基類和衍生類別。

public class Animal    {        public virtual void Write()        {            Console.WriteLine("我是基類");        }    }    public class Dog : Animal    {        public override void Write()        {            Console.WriteLine("我是小小狗");        }    }

 

為了讓衍生類別Dog隱式轉換成基類Animal,先定義支援協變的泛型介面。

//支援協變的介面    public interface IFactory<out T>    {        T Create();    }

 

再實現這個介面。

public class Factory<T> : IFactory<T>    {        public T Create()        {            return (T)Activator.CreateInstance<T>();        }    }

 

用戶端調用。

class Program    {        static void Main(string[] args)        {            IFactory<Dog> dogFactory = new Factory<Dog>();            IFactory<Animal> animalFactory = dogFactory; //協變            Animal animal = animalFactory.Create();            animal.Write();            Console.ReadKey();        }    }

 

運行輸出:我是小小狗

 

以上,我們可以看出:
● 協變後,父類的方法完全由子類替代,父類原先的方法不複存在
● 泛型介面中的out關鍵字必不可少


 

  泛型逆變

關於通知的一個介面。

public interface INotification    {        string Message { get; }    }

 

關於通知介面的抽象實現。

public abstract class Notification : INotification    {        public abstract string Message { get; }    }

 

關於通知抽象類別的具體實現。

public class MailNotification : Notification    {        public override string Message        {            get { return "你有郵件了~~"; }        }    }

 

接下來,需要把通知的資訊發布出去,需要一個發布通知的介面INotifier,該介面依賴INotification,大致INotifier<INotification>,而最終顯示通知,我們希望INotifier<MailNotification>,INotifier<INotification>轉換成INotifier<MailNotification>,這是逆變,需要關鍵字in。

public interface INotifier<in TNotification> where TNotification : INotification    {        void Notify(TNotification notification);    }

 

實現INotifier。

public class Notifier<TNotification> : INotifier<TNotification> where TNotification : INotification    {        public void Notify(TNotification notification)        {            Console.WriteLine(notification.Message);        }    }

 

用戶端調用。

class Program    {        static void Main(string[] args)        {            INotifier<INotification> notifier = new Notifier<INotification>();            INotifier<MailNotification> mailNotifier = notifier;//逆變            mailNotifier.Notify(new MailNotification());            Console.ReadKey();        }    }
   

運行輸出:你有郵件了~~

 

以上,我們可以看出:
● INotifier的方法Notify()的參數類型是INotification,逆變後把INotification型別參數隱式轉換成了實作類別MailNotificaiton。
● 泛型介面中的in關鍵字必不可少

 

參考資料:
《你必須知道的.NET(第2版)》,作者王濤。

 

".NET泛型"系列包括:

.NET泛型01,為什麼需要泛型,泛型基本文法.NET泛型02,泛型的使用.NET泛型03,泛型型別的轉換,協變和逆變

聯繫我們

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