C# 文法練習(11): 類[三] – 建構函式、解構函式、base、this

來源:互聯網
上載者:User
建構函式與解構函式:
using System;class MyClass{    private int FNum;    public int Num { get { return FNum; } }    /* 建構函式沒有傳回值, 無參的建構函式是預設的 */    public MyClass()    {        this.FNum = 2009;    }    /* 可以有多個參數不同的建構函式 */    public MyClass(int x)    {        this.FNum = x;    }    public MyClass(params int[] arr)    {        foreach (int i in arr) this.FNum += i;    }    /* 解構函式無參、無傳回值、無訪問修飾, 最多隻能有一個 */    ~MyClass()    {        //解構函式是自動調用的    }}class Program{    static void Main()    {        MyClass obj1, obj2, obj3;        obj1 = new MyClass();        Console.WriteLine(obj1.Num); //2009        obj2 = new MyClass(100);        Console.WriteLine(obj2.Num); //100        obj3 = new MyClass(1, 2, 3);        Console.WriteLine(obj3.Num); //6        Console.ReadKey();    }}

如果沒有構造與解構函式, new 時將使用預設(或繼承); 給一個私人的建構函式能阻止類被執行個體化:

using System;class MyClass{    private MyClass() { }    public static void Msg1() { Console.WriteLine("Msg1"); }    public static void Msg2() { Console.WriteLine("Msg2"); }}class Program{    static void Main()    {        MyClass.Msg1(); //Msg1        MyClass.Msg2(); //Msg2        Console.ReadKey();    }}

如果一個類有了非預設的建構函式, 就不能再使用預設的建構函式:

using System;class MyClass{    private int FNum;    public int Num { get { return FNum; } }    public MyClass(int x, int y)    {        this.FNum = x + y;    }}class Program{    static void Main()    {        MyClass obj;        obj = new MyClass(1, 2);        Console.WriteLine(obj.Num); //3        Console.ReadKey();    }}

靜態建構函式:

靜態建構函式既無存取修飾詞、無參數;
在 new 或調用任何靜態成員之前,將自動調用靜態建構函式;
靜態建構函式一般用於初始化待用資料;
靜態建構函式會在第一次 new 或第一次使用靜態成員前觸發;
不能直接調用靜態建構函式.

using System;class MyClass{    public static int Num;    public static void ShowNum() { Console.WriteLine(Num); }    public void Msg() { Console.WriteLine("Msg"); }    static MyClass() { Num = 123; }}class Program{    static void Main()    {        MyClass.ShowNum();            //123        MyClass.Num = 2009;        MyClass.ShowNum();            //2009        MyClass obj1 = new MyClass();        obj1.Msg();                   //Msg        Console.ReadKey();    }}

自動調用父類的構造方法:

using System;class Parent{    public Parent() { Console.WriteLine("Parent"); }}class Child1 : Parent{    public Child1() { Console.WriteLine("Child1"); }    public Child1(int x) { Console.WriteLine(x); }}class Child2 : Child1{    public Child2() { Console.WriteLine("Child2"); }    public Child2(int x, int y) { Console.WriteLine(x + y); }}class Program{    static void Main()    {        Parent p = new Parent();           // Parent        Child1 c1 = new Child1();          // Parent / Child1        Child2 c2 = new Child2();          // Parent / Child1 / Child2        Child1 c11 = new Child1(999);      // Parent / 999        Child2 c22 = new Child2(111, 222); // Parent / Child1 / 333                Console.ReadKey();    }}

base:

using System;class Parent{    public Parent() { Console.WriteLine("Parent"); }    public Parent(int x, int y) { Console.WriteLine(x + y); }    public Parent(string s1, string s2) { Console.WriteLine(s1 + s2); }}class Child1 : Parent{    public Child1() { Console.WriteLine("Child1"); }}class Child2 : Parent{    public Child2() : base() { Console.WriteLine("Child2"); }}class Child3 : Parent{    public Child3() : base(111,222) { Console.WriteLine("Child3"); }}class Child4 : Parent{    public Child4() : base("111", "222") { Console.WriteLine("Child4"); }}class Program{    static void Main()    {        Child1 c1 = new Child1(); // Parent / Child1        Child2 c2 = new Child2(); // Parent / Child2        Child3 c3 = new Child3(); // 333 / Child3        Child4 c4 = new Child4(); // 111222 / Child4                Console.ReadKey();    }}

this:

using System;class MyClass{    private string fs = "ABC-";    public MyClass() { Console.WriteLine("MyClass"); }    public MyClass(string str) { Console.WriteLine(this.fs + str); }    public MyClass(int num) : this() { Console.WriteLine(num); }    public MyClass(int x, int y) : this("XYZ") { Console.WriteLine(x + y); }}class Program{    static void Main()    {        MyClass c1 = new MyClass();          // MyClass        MyClass c2 = new MyClass("EFG");     // ABC-EFG        MyClass c3 = new MyClass(123);       // MyClass / 123        MyClass c4 = new MyClass(111, 222);  // ABC-XYZ / 333        Console.ReadKey();    }}

建構函式、屬性、base:

using System;abstract class Parent{    private byte FID;    public Parent(byte n)    {        FID = n;    }    public byte Id    {        get { return FID; }        set { FID = value; }    }}class Child : Parent{    public Child(byte MyID) : base(MyID) { }}class Program{    static void Main()    {        Child Rect = new Child(6);        Console.WriteLine(Rect.Id); //6        Rect.Id = 8;        Console.WriteLine(Rect.Id); //8        Console.ReadKey();    }}

相關文章

聯繫我們

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