C#封裝、多態、抽象、介面、匿名方法等學習

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   使用   ar   strong   

1:封裝

   將對象進行封裝,並不等於將整個對象完全包裹起來,而是根據實際需要,設定一定的存取權限,使用者根據不同的許可權調用對象提供的功能,在C#語言中,可以使用修飾符public、internal、protected、private分別修飾類的欄位、屬性和方法。

 

2:繼承,主要是注意繼承的格式

 

[csharp] view plaincopy
  1. public class ParentClass  //父類  
  2. {  
  3.    public ParentClass();  
  4. }  
  5.   
  6. public class ChildClass : ParentClass  :子類  
  7. {  
  8.    public ChildClass();  
  9. }  

 

 

3:多態

    多態是物件導向編程思想的核心之一,多態的意思是:當從父類派生出了一些子類後,每個子類都由不同的代碼實現,因此,同樣的調用可以產生不同的效果,主要由子類來決定,在程式運行時,物件導向的語言會自動判斷對象的衍生類別型,並調用相應的方法。

   在C#中多態分為兩種類型,即編譯時間多態和運行時多態,編譯時間多態是通過重載來實現的,運行時多態是系統運行時根據實際情況來決定何種操作。

執行個體代碼:

 

[html] view plaincopy
  1. public class MyIntertest   
  2. {  
  3.    public virtual void interest()   
  4.    {  
  5.       Console.WriteLine("我的愛好");  
  6.       Console.ReadLine();  
  7.    }  
  8. }  
  9.   
  10. public class Writer : MyIntertest  
  11. {  
  12.     public override void Interest()   
  13.     {  
  14.        Console.WriterLine("寫作");  
  15.     }  
  16. }  
  17.   
  18. public class Program : MyInterest  
  19. {  
  20.     public override void interest()   
  21.     {  
  22.         Console.WriteLine("編程");  
  23.     }  
  24. }  
  25.   
  26. public class Sing : MyInterest   
  27. {  
  28.     public override void interest()   
  29.     {  
  30.         Console.WriteLine("唱歌");  
  31.     }  
  32. }  
  33.   
  34.   
  35. public class Test   
  36. {  
  37.    public static int main(String[] args)   
  38.    {  
  39.        MyInterest[] interests = new MyInterest[4];  
  40.        interests[0] = new Write();  
  41.        interests[1] = new Program();  
  42.        interests[2] = new Sing();  
  43.        interests[3] = new MyInterest()  
  44.          
  45.        foreach ( MyInterest interest in interests)  
  46.        {  
  47.            interest.interest();  
  48.        }  
  49.          
  50.        return 0;  
  51.    }  
  52. }  

 

4:

介面:    介面,是兩個有界事物的交界處,通過介面可以與對方互動,互動的方式由約定產生,這種約定就是由具體互動動作的規則,只要遵循規則 即可與擁有規則的事物互動。    在C#中,介面是聲明類具有的方法和成員的、自身不能被執行個體化的、只能被繼承的特殊的類,其只管描述事物的特性而實現特性的任務卻由其他類負責。    介面中不能包含欄位    介面定義了類要實現的一系列方法,但是它本身不能實現這些方法,只是用邏輯結構方式來描述。        在介面的成員中,只能是方法、屬性、事件或者索引器,它的成員不能包含常數、欄位、運算子、執行個體建構函式、析構建構函式或類型,也不能包含任意種類的    靜態成員。    

[csharp] view plaincopy
  1. interface MyInterface  
  2.   {  
  3.      string STR    //屬性STR  
  4.      {  
  5.        get;       //屬性可讀  
  6.        set;       //屬性可寫  
  7.      }      
  8.      void outMethod();   //成員方法outMethod  
  9.   }  
  10.     
  11.   class InheritInterface : MyInterface  
  12.   {  
  13.      string str = "21天學習C#";    //定義並初始化字串  
  14.      public string STR  
  15.      {  
  16.         get  
  17.         {  
  18.            return str;  
  19.         }  
  20.         set  
  21.         {  
  22.            str = value;  
  23.         }  
  24.      }    
  25.      public void outMethod()  
  26.      {  
  27.         Console.WriteLine(this.STR);  
  28.      }   
  29.   }  



 

5: 域和屬性     域也叫成員變數,它可以用來儲存類的各種資訊,域可以分為靜態域和執行個體域兩種,其中,靜態域屬於類,執行個體域屬於對象。     屬性是一種用來訪問對象的特殊成員。          域的聲明:和普通變數相同,可以用new, public, protected, static和readonly等         

[csharp] view plaincopy
  1. public class Date  
  2.    {  
  3.       private string name = "hoan";  
  4.       public string nameAttr   
  5.       {  
  6.         get  //使用get訪問器讀值  
  7.         {  
  8.            return name;  
  9.         }  
  10.         set  //使用set訪問器寫值  
  11.         {  
  12.              
  13.         }  
  14.       }  
  15.    }  


6:匿名方法:
   匿名方法的作用是將代碼塊當作參數來使用,使代碼對於委託的執行個體很直接,很便利,可以減少執行個體化委託對系統資源的開銷。匿名方法還共用了
   本地語句包含的函數成員存取權限。
   
   匿名方法的使用:
    

[csharp] view plaincopy
  1. namespace codemo  
  2.     {  
  3.        delegate void Writer(string s);  //委託所調用的命名方法  
  4.        class Program   
  5.        {  
  6.            static void NamedMethod(string k)   //委託所調用的命名方法  
  7.            {  
  8.               System.Console.WriteLine(k);   
  9.            }  
  10.            static void Main(string[] args)   
  11.            {  
  12.               Writer w = delegate(string j)  //委託類調用匿名方法  
  13.               {  
  14.                  System.Console.WriteLine(j);  
  15.               }  
  16.               w("調用了匿名方法");  
  17.               Console.WriteLine();  
  18.               w = new Writer(NamedMethod);  //建立對象  
  19.               w("調用了命名方法");          //調用命名方法  
  20.               Console.ReadLine();       
  21.            }   
  22.        }   
  23.     }  

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.