1、什麼是構造函數? 有哪些構造函數? 各個構造函數的定義、實現方法、注意事項?
所謂構造函數,就是一個方法,這個方法可以初始化對象,即運行完這個函數後,內存總開辟了一塊該類的對象的空間。有三種:正常的構造函數,也就是實例化構造函數;私有構造函數;靜態構造函數。
實例化構造器:
public class Example
{
private string property1 = string.Empty;
private string property2 = @"hello";
private int property3 = 0;
public Example()//成員都是聲明時的初始值,這種默認的構造器,也可以不寫。
{
}
public Example(string p1, string p2, int p3)//傳入的值初始化
{
this.property1 = p1;
this.property2 = p2;
this.property3 = p3;
}
}
public class Example
{
private string property1 = string.Empty;
private string property2 = @"hello";
private int property3 = 0;
public Example()//成員都是聲明時的初始值,這種默認的構造器,也可以不寫。
{
}
public Example(string p1, string p2, int p3)//傳入的值初始化
{
this.property1 = p1;
this.property2 = p2;
this.property3 = p3;
}
}
私有構造器:
私有構造器,外部是不能訪問的,那麼如何實例化呢,參見單例模式,這裡就是用了私有構造函數:
靜態構造函數:
先看例子:
public class StaticConstruct
{
static StaticConstruct()
{
Console.WriteLine(@"靜態構造函數");
}
public StaticConstruct()
{
Console.WriteLine(@"實例化構造函數");
}
public StaticConstruct(string flage)
{
Console.WriteLine(@"帶參構造函數");
}
}
class Program
{
static void Main(string[] args)
{
StaticConstruct strc = new StaticConstruct();
StaticConstruct strcValue = new StaticConstruct(string.Empty);
Console.ReadLine();
}
}
public class StaticConstruct
{
static StaticConstruct()
{
Console.WriteLine(@"靜態構造函數");
}
public StaticConstruct()
{
Console.WriteLine(@"實例化構造函數");
}
public StaticConstruct(string flage)
{
Console.WriteLine(@"帶參構造函數");
}
}
class Program
{
static void Main(string[] args)
{
StaticConstruct strc = new StaticConstruct();
StaticConstruct strcValue = new StaticConstruct(string.Empty);
Console.ReadLine();
}
}
結果:
靜態構造函數特點:靜態構造函數中不允許出現訪問修飾符;實例化的時候,首先自動調用靜態構造函數,意即調用靜態構造函數是不可控的;靜態構造函數是無參的,並且一個類中只有一個;不能被繼承。
2、This關鍵字和Base關鍵字用途? 實現代碼?
(1)、this關鍵字:
this顧名思義,就是指本類中的意思,引用當前類的成員。當然如果程序在運行中,則可以精確地說,this指當前類的對象的成員,作用就是用來區分對象的。因為一個類可以有N個對象。不過在static類中不能使用this關鍵字,究其原因,無非是static不可能實例化多個對象,它只有一個,自然沒必要去用this來區分對象了。一般常用如下:
a、方法或構造函數中,同名變量。
public class MyTestA
private string testA = string.Empty;
public MyTestA(string testA)
{
this.testA = testA;
}
public void Handler(string testA)
{
this.testA = testA;
}
public class MyTestA
{
private string testA = string.Empty;
public MyTestA(string testA)
{
this.testA = testA;
}
public void Handler(string testA)
{
this.testA = testA;
}
}
b、get,set方法
public class MyTestB
{
private string testB = string.Empty;
public string TestB
{
get
{
return this.testB;
}
set
{
this.testB = value;
}
}
}
public class MyTestB
{
private string testB = string.Empty;
public string TestB
{
get
{
return this.testB;
}
set
{
this.testB = value;
}
}
}
c、將實例傳遞
比如,事件中
public class MyTestC
{
public event EventHandler OnTestCEvent = null;
private void Send_OntestEvent(object sender,EventArgs e)
{
if (OnTestCEvent != null)
{
OnTestCEvent(sender, e);
}
}
private void TestEvent()
{
Send_OntestEvent(this, null);
}
}
public class MyTestD
{
MyTestC testC = new MyTestC();
public event EventHandler OnTestDEvent = null;
private void Send_OnTestDEvent(object sender, EventArgs e)
{
if (OnTestDEvent != null)
{
OnTestDEvent(sender, e);
}
}
public MyTestD()
{
testC.OnTestCEvent += new EventHandler(testC_OnTestEvent);
}
void testC_OnTestEvent(object sender, EventArgs e)
{
Send_OnTestDEvent(sender, e);
}
}
public class MyTestE
{
MyTestD testD = new MyTestD();
public MyTestE()
{
this.testD.OnTestDEvent += new EventHandler(testD_OnTestDEvent);
}
void testD_OnTestDEvent(object sender, EventArgs e)
{
MyTestC testC = sender as MyTestC;//通過MytestD將對象轉了過來
if (testC != null)
{
//代碼
}
}
}
public class MyTestC
{
public event EventHandler OnTestCEvent = null;
private void Send_OntestEvent(object sender,EventArgs e)
{
if (OnTestCEvent != null)
{
OnTestCEvent(sender, e);
}
}
private void TestEvent()
{
Send_OntestEvent(this, null);
}
}
public class MyTestD
{
MyTestC testC = new MyTestC();
public event EventHandler OnTestDEvent = null;
private void Send_OnTestDEvent(object sender, EventArgs e)
{
if (OnTestDEvent != null)
{
OnTestDEvent(sender, e);
}
}
public MyTestD()
{
testC.OnTestCEvent += new EventHandler(testC_OnTestEvent);
}
void testC_OnTestEvent(object sender, EventArgs e)
{
Send_OnTestDEvent(sender, e);
}
}
public class MyTestE
{
MyTestD testD = new MyTestD();
public MyTestE()
{
this.testD.OnTestDEvent += new EventHandler(testD_OnTestDEvent);
}
void testD_OnTestDEvent(object sender, EventArgs e)
{
MyTestC testC = sender as MyTestC;//通過MytestD將對象轉了過來
if (testC != null)
{
//代碼
}
}
}
(2)base關鍵字:
一般用於,子類訪問父類。
一種是,重寫父類方法時,
public class ParentClass
{
public virtual void MethodA()
{
Console.WriteLine(@"基類的方法");
}
}
public class ChildClass : ParentClass
{
public override void MethodA()
{
base.MethodA();
Console.WriteLine("派生類方法");
}
}
public class ParentClass
{
public virtual void MethodA()
{
Console.WriteLine(@"基類的方法");
}
}
public class ChildClass : ParentClass
{
public override void MethodA()
{
base.MethodA();
Console.WriteLine("派生類方法");
}
}
另一種,子類調用父類構造函數,
public class ParentClass
{
public ParentClass(string flage)
{
Console.WriteLine(@"基類構造函數");
}
public virtual void MethodA()
{
Console.WriteLine(@"基類的方法");
}
}
public class ChildClass : ParentClass
{
public ChildClass(string flage)
: base(flage)
{
} www.2cto.com
public override void MethodA()
{
base.MethodA();
Console.WriteLine("派生類方法");
}
}
public class ParentClass
{
public ParentClass(string flage)
{
Console.WriteLine(@"基類構造函數");
}
public virtual void MethodA()
{
Console.WriteLine(@"基類的方法");
}
}
public class ChildClass : ParentClass
{
public ChildClass(string flage)
: base(flage)
{
}
public override void MethodA()
{
base.MethodA();
Console.WriteLine("派生類方法");
}
}
3、什麼是反射? 如何實現反射? 反射有何優缺點? 何時使用反射?
http://www.BkJia.com/kf/201206/136302.html
作者 白楊樹