構造函數與析構函數:
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();
}
}