C#介面

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;/* * 1、介面表示一組函數成員而不實現成員的參考型別 * 2、只有類和結構可以實現介面,必須實現介面中的全部成員 * 3、介面可以繼承介面 * 4、將類強制轉換為類引用為實現的介面的引用時,會拋出異常;用 as 運算子來避免,轉換不成功則值為null *      ILiveBirth lb =a as ILiveBirth *      if(lb!=null) * 5、類可以實現任意數量的介面,這些介面成員有重複時,類可以實現單個成員來滿足所有包含重複成員的介面。 * 也可以建立明確介面實作,在實現成員時,限定介面名 *  *  */namespace ExInterFace{    interface IIfc    {        void PrintOut(string s);    }    class MyClass : IIfc     {        public void PrintOut(string s)        {            Console.WriteLine("Calling through {0}",s);        }    }    class Program    {        static void Main(string[] args)        {            MyClass mc = new MyClass();            mc.PrintOut("Object");            IIfc ifc1 = (IIfc)mc;            ifc1.PrintOut("Interface");            Console.ReadLine();        }    }}

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.