C#之建構函式
建構函式分為: 1.例項建構函式 2.私有建構函式 3.靜態建構函式 建構函式是一種特殊的方法,主要是為了給初始化物件賦初值。 1.例項建構函式
使用new 表示式建立某個類 的物件時,會使用例項建構函式建立和初始化所有例項成員變數。
public class ProgramTest { int j; public ProgramTest() { j = 4 ; Console.WriteLine( " I am ProgramTest,{0} " , j ); } static void Main( string [] args) { ProgramTest pt = new ProgramTest(); Console.Read(); }
結果為:I am ProgramTest,4 在此次實驗中,我們首先定義了一個私有成員j,經過初始化給他賦了一個初值4,當我例項化類ProgramTest時,就會執行例項建構函式。
諸如此類不帶引數的建構函式叫“預設建構函式”, 如果某個類沒有建構函式,則會自動生成一個預設建構函式,並使用預設值來初始化物件欄位。
public class ProgramTest { int j; public ProgramTest() { Console.WriteLine( " I am ProgramTest,{0} " , j); } static void Main( string [] args) { ProgramTest pt = new ProgramTest(); Console.Read(); } } 結果為:I am ProgramTest, 0 也可以建立帶有引數的建構函式 public class ProgramTest { int j; public ProgramTest( int i) { j = 2 ; Console.WriteLine( " I am ProgramTest,i={0},j={1} " ,i, j); } static void Main( string [] args) { ProgramTest pt = new ProgramTest( 1 ); Console.Read(); }
結果為:I am ProgramTest i=1,j= 2
那若是一個類中既有無參建構函式,又有有參建構函式那會怎麼樣呢?
public class ProgramTest { int j; public ProgramTest() { j = 3 ; Console.WriteLine( " I am ProgramTest 預設建構函式,j={0} " , j); } public ProgramTest( int i) { j = 2 ; Console.WriteLine( " I am ProgramTest 有參建構函式,i={0},j={1} " ,i, j); } static void Main( string [] args) { ProgramTest pt1 = new ProgramTest(); ProgramTest pt2 = new ProgramTest( 1 ); Console.Read(); } 結果為:I am ProgramTest 預設建構函式 j= 3 I am ProgramTest 有參建構函式 i=1,j=2
可見,若是兩者同時存在,那麼看類例項化時,是怎麼例項的。 2.私有建構函式
私有建構函式是一種特殊的例項建構函式。 它通常用在只包含靜態成員的類中。
如果類具有一個或多個私有建構函式而沒有公共建構函式,則其他類(除巢狀類外)無法建立該類的例項。
public
class
Test
{
private
Test()
{
Console.WriteLine(
"
I am Test
"
);
}
}
public
class
ProgramTest
{
int
j;
private
ProgramTest()
{
j
=
3
;
Console.WriteLine(
"
I am ProgramTest 預設建構函式,j={0}
"
, j);
}
static
void
Main(
string
[] args)
{
Test t
=
new
Test();
ProgramTest pt1
=
new
ProgramTest();
Console.Read();
}
}
結果:這時你會發現編譯器會提示你,你無法建立Test的例項 (即使你在Test類中新增一個靜態成員,結果依然會提示因為該建構函式受其保護級別的限制而不可訪問)
若是隻在自己類裡有私有類呢?
public
class
ProgramTest
{
int
j;
private
ProgramTest()
{
j
=
3
;
Console.WriteLine(
"
I am ProgramTest 預設建構函式,j={0}
"
, j);
}
static
void
Main(
string
[] args)
{
ProgramTest pt1
=
new
ProgramTest();
Console.Read();
}
}
結果:I am ProgranmTest 預設建構函式,j=3
注意,如果您不對建構函式使用訪問修飾符,則在預設情況下它仍為私有建構函式。但是,通常顯式地使用private修飾符來清楚地表明該類不能被例項化。 若是一個類中既有例項建構函式又有私有建構函式,那麼當例項物件是會怎麼執行呢?
public
class
Test
{
int
i;
private
Test()
{
i
=
1
;
Console.WriteLine(
"
I am Test 預設建構函式 i={0}
"
, i);
}
public
Test(
int
i)
{
Console.WriteLine(
"
I am Test 有參建構函式 i={0}
"
, i);
}
}
public
class
ProgramTest
{
static
void
Main(
string
[] args)
{
Test t
=
new
Test();
//編譯器提示:因為該建構函式受其保護級別的限制而不可訪問
Console.Read();
若是隻執行有參建構函式呢?
public
class
Test
{
int
i;
private
Test()
{
i
=
1
;
Console.WriteLine(
"
I am Test 預設建構函式 i={0}
"
, i);
}
public
Test(
int
i)
{
Console.WriteLine(
"
I am Test 有參建構函式 i={0}
"
, i);
}
}
public
class
ProgramTest
{
static
void
Main(
string
[] args)
{
Test t
=
new
Test(
2
);
Console.Read();
}
}
結果:I am Test 有參建構函式 i=2
3.靜態建構函式 靜態建構函式用來初始化靜態變數,這個建構函式是屬於類的,而不是屬於哪個例項的。
就是說這個建構函式只會被執行一次。也就是在建立第一個例項或引用任何靜態成員之前,由.NET自動呼叫。
public
class
Test
{
static
int
i;
static
Test()
{
i
=
1
;
Console.WriteLine(
"
I am Test 預設建構函式 i={0}
"
, i);
}
}
public
class
ProgramTest
{
static
void
Main(
string
[] args)
{
Test t1
=
new
Test();
Console.Read();
}
}
結果為:I am Test 預設建構函式 i=1
靜態建構函式的特點:
1.靜態建構函式既沒有訪問修飾符,也沒有引數。
在建立第一個例項或引用任何靜態成員之前,將自動呼叫靜態建構函式來初始化類,也就是無法直接呼叫靜態建構函式,也無法控制什麼時候執行靜態建構函式。
3.一個類只能有一個靜態建構函式,最多隻能執行一次。 4.靜態建構函式不可以被繼承。 5.如果沒有靜態建構函式,而類中的靜態成員有初始值,那麼編譯器會自動生成預設的靜態建構函式。 如果靜態預設建構函式和公有預設建構函式同時存在,會怎麼樣呢?
public
class
Test
{
static
int
i;
static
Test()
{
i
=
1
;
Console.WriteLine(
"
I am Test 靜態預設建構函式 i={0}
"
, i);
}
public
Test()
{
Console.WriteLine(
"
I am Test 公有預設建構函式 i={0}
"
, i);
}
}
public
class
ProgramTest
{
static
void
Main(
string
[] args)
{
Test t1
=
new
Test();
Console.Read();
}
}
結果:I am Test 靜態預設建構函式 i=1
I am Test 公有預設建構函式 i=1
如果靜態預設建構函式和公有有參建構函式同時存在,我例項化類讓它呼叫靜態預設建構函式會怎麼樣呢?
public
class
Test
{
static
int
i;
static
Test()
{
i
=
1
;
Console.WriteLine(
"
I am Test 靜態預設建構函式 i={0}
"
, i);
}
public
Test(
int
j)
{
Console.WriteLine(
"
I am Test 公有有參建構函式 i={0}
"
, j);
}
}
public
class
ProgramTest
{
static
void
Main(
string
[] args)
{
Test t1
=
new
Test();
//系統會提示錯誤
Console.Read();
}
}
如果靜態預設建構函式和公有有參建構函式同時存在,我例項化類讓它呼叫公有建構函式會怎麼樣呢?
public
class
Test
{
static
int
i;
static
Test()
{
i
=
1
;
Console.WriteLine(
"
I am Test 靜態預設建構函式 i={0}
"
, i);
}
public
Test(
int
j)
{
Console.WriteLine(
"
I am Test 公有有參建構函式 i={0}
"
, j);
}
}
public
class
ProgramTest
{
static
void
Main(
string
[] args)
{
Test t1
=
new
Test(
2
);
Console.Read();
}
}
結果:I am Test 靜態預設建構函式 i=1 I am Test 公有有參建構函式 j=2