介面--interface

來源:互聯網
上載者:User

標籤:使用   ar   sp   on   c   代碼   ad   line   r   

概述:C#中的類不支援多重繼承,但可以通過介面來實現多重繼承的功能。

         介面的4種成員類型分別為:方法、屬性、事件、索引器,但是只能定義,不能設定它們的具體值,且介面中不包含方法的具體實現

          特別注意不包含欄位

特徵:類似於抽象基類,即繼承介面的任何非抽象類別型都必須實現介面的所有成員

         類和結構可從多個介面繼承

         介面自身可從多個介面繼承

聲明:修飾符  interface  介面名稱:繼承的介面列表

          {   介面內容  }

          註:介面聲明時,除interface關鍵字和介面名稱外,其他都是可以預設的

                可以用new.public.protected.internal.private等修飾符聲明介面,但介面成員必須是public

介面的實現與繼承:

namespace _Interface

{   

  interface IPeople    

{        

string Name { get; set; }        

string Sex { get; set; }         

}    

interface ITeacher : IPeople  //繼承公用介面    

{        

void teach();    

}    

interface IStudent : IPeople    

{      

   void study();    

}

 class Program:IPeople ,ITeacher ,IStudent    //多介面繼承   

  {         string name = "";

        public string Name         {             get { return name; }             set { name = value; }         }        

string sex = "";

        public string Sex         {             get { return sex; }             set { sex = value; }       

  }        

public void teach()        

{            

Console.WriteLine(Name +""+ Sex+"教師");        

}        

public void study()     

    {       

      Console.WriteLine(Name + "" + Sex + "學生");     

    }        

static void Main(string[] args)     

    {          

   Program program = new Program();//執行個體化對象      

       ITeacher iteacher = program;    //使用衍生類別對象執行個體化介面ITeacher            

iteacher.Name = "TM";           

  iteacher.Sex = "男";            

iteacher.teach();           

IStudent istudent = program;    //使用衍生類別對象執行個體化介面IStuent            

istudent.Name = "C#";            

istudent.Sex = "男";          

   istudent.study();            

Console.ReadLine();        

}     } }

顯示介面成員的實現:

            如果類實現兩個介面,且兩個介面包含具有相同簽名的成員,那麼在類中實現 該成員將導致兩個介面都使用該成員作為它們的實現。若兩個介面成員實現不同的功能,則可能出錯。此時可以顯示地實現介面成員,即建立一個僅通過該介面調用並且特定於該介面的成員。顯示介面成員實現是使用介面名稱和一個句點命名該類成員來實現的。

範例程式碼如下:namespace _顯示介面的實現
{
    interface myInterface1
    {
        int Add();
    }
    interface myInterface2
    {
        int Add();
    }
    class myClass : myInterface1, myInterface2
    {       
        int myInterface1.Add()   //顯示介面成員的實現
        {
            int x = 3;
            int y = 5;
            return x + y;
        }
        int myInterface2.Add()   //顯示介面成員的實現
        {
            int x = 3;
            int y = 5;
            int z = 7;
            return x+y+z;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            myClass myclass=new myClass ();   //執行個體化介面繼承類的對象
            myInterface1 myInterface1=myclass ;   //使用介面繼承類的對象執行個體化介面
            Console.WriteLine(myInterface1.Add());  //使用介面對象調用介面中的方法
            myInterface2 myInterface2 = myclass;
            Console.WriteLine(myInterface2 .Add());
            Console.ReadLine();
        }
    }

}

介面--interface

聯繫我們

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