C#基礎系列-介面

來源:互聯網
上載者:User

標籤:

前言: 完全是我在學習過程中記錄的筆記,只不過分享一下讓很多剛開始學習.net編程的人能夠很快的學會C#語言

  1. 介面

 (1) 有時候需要考慮子類中一部分具有某些能力(方法),同時又需要考慮其多態的特徵,這時候可以考慮介面

 (2)文法:

            public interface 介面名

            {

                   //成員

            }

 (3) 介面的存在就是為了被實現

 (4)有了介面以後實現該介面的子類可以賦值給這個介面的變數,就可以實現多態了

 (5)介面的存在就是為了多態

 (6)介面與類的其中一個不同點在於多"繼承"(實現)

 (7)介面的使用

    interface IDriveable

              {

                     void Dirving();

              }

              class Teacher : IDriveable

              {

                     public void Dirving()

                     {

                            Console.WriteLine("終於有駕駛證了");

                     }

              }

              class Program

              {

                     static void Main(string[] args)

                     {

                            Teacher th = new Teacher();

                            th.Dirving();

                            IDriveable dir = th;

                            dir.Dirving();

                     }

              }

(8)介面的補充

       1)介面中的方法

              ->預設介面成員均是public,所以不需要考慮存取修飾詞

              ->直接不寫存取修飾詞

                     傳回值 方法名 (參數);

       2)介面的思想類中實現方法的文法:方法該怎麼寫就怎麼寫,好像沒有介面存在一樣

       3)介面的實作類別必須實現介面中的成員

   4)介面中可以有哪些成員:抽象成員

 (9)在使用上介面與抽象類別非常類似(但是介面不允許有非抽象成員,成員的文法定義不同,多繼承)

 (10)在繼承中有介面,又有類的時候注意:

       1)文法

              class 類名:父類,介面,介面

              {

              //成員

              }

  2)多個介面中如果有重名方法怎麼辦?

              ->使用顯示實現介面的方式

              ->方法名是有介面名引導的

              ->方法沒有存取修飾詞

              ->方法不能被該類型的執行個體對象說調用,必須將其轉換為介面類型進行調用

          

介面的實現
interface Iinter1

{

void Func();

}

interface Iinter2

{

void Func();

}

class MyClass : Iinter1, Iinter2

{

void Iinter1.Func()

{

Console.WriteLine("介面1");

}

void Iinter2.Func()

{

Console.WriteLine("介面2");

}

}

class Program

{

static void Main(string[] args)

{

MyClass m = new MyClass();

Iinter1 i1 = m;

i1.Func();

Iinter2 i2 = m;

i2.Func();

}

}


 1                   interface Iinter1 2  3                             { 4  5                                    void Func(); 6  7                             } 8  9                             interface Iinter210 11                             {12 13                                    void Func();14 15                             }16 17                             class MyClass : Iinter1, Iinter218 19                             {20 21                                    void Iinter1.Func()22 23                                    {24 25                                           Console.WriteLine("介面1");26 27                                    }28 29                                    void Iinter2.Func()30 31                                    {32 33                                           Console.WriteLine("介面2");34 35                                    }36 37                             }38 39                             class Program40 41                             {42 43                                    static void Main(string[] args)44 45                                    {46 47                                           MyClass m = new MyClass();48 49                                           Iinter1 i1 = m;50 51                                           i1.Func();52 53                                           Iinter2 i2 = m;54 55                                           i2.Func();56 57                                    }58 59                             }

 

  1. 使用介面模仿一個山寨的暗黑遊戲的介面原理

  

介面實現暗黑遊戲的一點山寨 (1) 首先建立一個控制台應用程式,自己隨便起個名字 (2)在建立一個介面,起名為interfaceModel,這個介面實現了攻擊的種類            interface IFile //火系攻擊            {                   void Fight();            }            interface IIce //冰系攻擊            {                   void Fight();            } (3)其次建立一個可以實現所有攻擊方法的類            /// <summary>            /// 一個可以實現所有方法的類            /// </summary>            abstract class Person            {                   public abstract void Gongji();            } (4)建立一個fashi類,此類實現了法師的所有的攻擊手段         abstract class fashi : Person            {            }            class fashi1 : fashi, IFile            {                   public void Fight()                   {                          Console.WriteLine("火攻");                   }                   public override void Gongji()                   {                          Fight();                   }            }            class fashi2 : fashi, IIce            {                   public void Fight()                   {                          Console.WriteLine("冰攻");                   }                   public override void Gongji()                   {                          Fight();                   }            }            class fashi3 : fashi, IFile, IIce            {                   public override void Gongji()                   {                          //隨機的攻擊                          Random rand = new Random();                          if (rand.Next() % 2 == 0)                          {                                 IIce i = this as IIce;                                 i.Fight();                          }                          else                          {                                 (this as IFile).Fight();                          }                   }                   void IFile.Fight()                   {                          Console.WriteLine("火攻");                   }                   void IIce.Fight()                   {                          Console.WriteLine("冰攻");                   }            }     (5)在main方法中實現如下函數            static void Main(string[] args)        {            fashi f = new fashi3();            while (true)            {                f.Gongji();                Console.ReadKey();            }        }

  

 

 (6)在定義方法的時候,可以為變數賦初值,該值為預設值,如果不傳參數就預設使用這個值,如果傳參,使用傳進來的參數替代這個值

 (7)微軟推薦使用預設參數而非重載

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.