C# 類 Class

c# 類 class

當(dāng)你定義一個(gè)類時(shí),你定義了一個(gè)數(shù)據(jù)類型的藍(lán)圖。這實(shí)際上并沒有定義任何的數(shù)據(jù),但它定義了類的名稱意味著什么,也就是說(shuō),類的對(duì)象由什么組成及在這個(gè)對(duì)象上可執(zhí)行什么操作。對(duì)象是類的范例。構(gòu)成類的方法和變量稱為類的成員。

 

1. 類的定義

類的定義是以關(guān)鍵字 class 開始,后跟類的名稱。類的主體,包含在一對(duì)花括號(hào)內(nèi)。下面是類定義的一般形式:

<access specifier> class ?class_name 
{
? ? // member variables
? ? <access specifier> <data type> variable1;
? ? <access specifier> <data type> variable2;
? ? ...
? ? <access specifier> <data type> variablen;
? ? // member methods
? ? <access specifier> <return type> method1(parameter_list) 
? ? {
? ? ? ? // method body 
? ? }
? ? <access specifier> <return type> method2(parameter_list) 
? ? {
? ? ? ? // method body 
? ? }
? ? ...
? ? <access specifier> <return type> methodn(parameter_list) 
? ? {
? ? ? ? // method body 
? ? }
}

請(qǐng)注意:

  • 訪問(wèn)標(biāo)識(shí)符 <access specifier> 指定了對(duì)類及其成員的訪問(wèn)規(guī)則。如果沒有指定,則使用默認(rèn)的訪問(wèn)標(biāo)識(shí)符。類的默認(rèn)訪問(wèn)標(biāo)識(shí)符是 internal,成員的默認(rèn)訪問(wèn)標(biāo)識(shí)符是 private。
  • 數(shù)據(jù)類型 <data type> 指定了變量的類型,返回類型 <return type> 指定了返回的方法返回的數(shù)據(jù)類型。
  • 如果要訪問(wèn)類的成員,你要使用點(diǎn)(.)運(yùn)算符。
  • 點(diǎn)運(yùn)算符鏈接了對(duì)象的名稱和成員的名稱。

下面的范例說(shuō)明了目前為止所討論的概念:

using system;
namespace boxapplication
{
? ? class box
? ? {
? ? ? ?public double length; ? // 長(zhǎng)度
? ? ? ?public double breadth; ?// 寬度
? ? ? ?public double height; ? // 高度
? ? }
? ? class boxtester
? ? {
? ? ? ? static void main(string[] args)
? ? ? ? {
? ? ? ? ? ? box box1 = new box(); ? ? ? ?// 聲明 box1,類型為 box
? ? ? ? ? ? box box2 = new box(); ? ? ? ?// 聲明 box2,類型為 box
? ? ? ? ? ? double volume = 0.0; ? ? ? ? // 體積

? ? ? ? ? ? // box1 詳述
? ? ? ? ? ? box1.height = 5.0;
? ? ? ? ? ? box1.length = 6.0;
? ? ? ? ? ? box1.breadth = 7.0;

? ? ? ? ? ? // box2 詳述
? ? ? ? ? ? box2.height = 10.0;
? ? ? ? ? ? box2.length = 12.0;
? ? ? ? ? ? box2.breadth = 13.0;
? ? ? ? ? ?
? ? ? ? ? ? // box1 的體積
? ? ? ? ? ? volume = box1.height * box1.length * box1.breadth;
? ? ? ? ? ? console.writeline("box1 的體積: {0}", ?volume);

? ? ? ? ? ? // box2 的體積
? ? ? ? ? ? volume = box2.height * box2.length * box2.breadth;
? ? ? ? ? ? console.writeline("box2 的體積: {0}", volume);
? ? ? ? ? ? console.readkey();
? ? ? ? }
? ? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

box1 的體積: 210
box2 的體積: 1560

 

2. 成員函數(shù)和封裝

類的成員函數(shù)是一個(gè)在類定義中有它的定義或原型的函數(shù),就像其他變量一樣。作為類的一個(gè)成員,它能在類的任何對(duì)象上操作,且能訪問(wèn)該對(duì)象的類的所有成員。

成員變量是對(duì)象的屬性(從設(shè)計(jì)角度),且它們保持私有來(lái)實(shí)現(xiàn)封裝。這些變量只能使用公共成員函數(shù)來(lái)訪問(wèn)。

讓我們使用上面的概念來(lái)設(shè)置和獲取一個(gè)類中不同的類成員的值:

using system;
namespace boxapplication
{
? ? class box
? ? {
? ? ? ?private double length; ? // 長(zhǎng)度
? ? ? ?private double breadth; ?// 寬度
? ? ? ?private double height; ? // 高度
? ? ? ?public void setlength( double len )
? ? ? ?{
? ? ? ? ? ? length = len;
? ? ? ?}

? ? ? ?public void setbreadth( double bre )
? ? ? ?{
? ? ? ? ? ? breadth = bre;
? ? ? ?}

? ? ? ?public void setheight( double hei )
? ? ? ?{
? ? ? ? ? ? height = hei;
? ? ? ?}
? ? ? ?public double getvolume()
? ? ? ?{
? ? ? ? ? ?return length * breadth * height;
? ? ? ?}
? ? }
? ? class boxtester
? ? {
? ? ? ? static void main(string[] args)
? ? ? ? {
? ? ? ? ? ? box box1 = new box(); ? ? ? ?// 聲明 box1,類型為 box
? ? ? ? ? ? box box2 = new box(); ? ? ? ? ? ? ? ?// 聲明 box2,類型為 box
? ? ? ? ? ? double volume;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 體積


? ? ? ? ? ? // box1 詳述
? ? ? ? ? ? box1.setlength(6.0);
? ? ? ? ? ? box1.setbreadth(7.0);
? ? ? ? ? ? box1.setheight(5.0);

? ? ? ? ? ? // box2 詳述
? ? ? ? ? ? box2.setlength(12.0);
? ? ? ? ? ? box2.setbreadth(13.0);
? ? ? ? ? ? box2.setheight(10.0);
? ? ? ?
? ? ? ? ? ? // box1 的體積
? ? ? ? ? ? volume = box1.getvolume();
? ? ? ? ? ? console.writeline("box1 的體積: {0}" ,volume);

? ? ? ? ? ? // box2 的體積
? ? ? ? ? ? volume = box2.getvolume();
? ? ? ? ? ? console.writeline("box2 的體積: {0}", volume);
? ? ? ? ? ?
? ? ? ? ? ? console.readkey();
? ? ? ? }
? ? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

box1 的體積: 210
box2 的體積: 1560

 

3. c# 中的構(gòu)造函數(shù)

類的 構(gòu)造函數(shù) 是類的一個(gè)特殊的成員函數(shù),當(dāng)創(chuàng)建類的新對(duì)象時(shí)執(zhí)行。

構(gòu)造函數(shù)的名稱與類的名稱完全相同,它沒有任何返回類型。

下面的范例說(shuō)明了構(gòu)造函數(shù)的概念:

using system;
namespace lineapplication
{
? ?class line
? ?{
? ? ? private double length; ? // 線條的長(zhǎng)度
? ? ? public line()
? ? ? {
? ? ? ? ?console.writeline("對(duì)象已創(chuàng)建");
? ? ? }

? ? ? public void setlength( double len )
? ? ? {
? ? ? ? ?length = len;
? ? ? }
? ? ? public double getlength()
? ? ? {
? ? ? ? ?return length;
? ? ? }

? ? ? static void main(string[] args)
? ? ? {
? ? ? ? ?line line = new line(); ? ?
? ? ? ? ?// 設(shè)置線條長(zhǎng)度
? ? ? ? ?line.setlength(6.0);
? ? ? ? ?console.writeline("線條的長(zhǎng)度: {0}", line.getlength());
? ? ? ? ?console.readkey();
? ? ? }
? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

對(duì)象已創(chuàng)建
線條的長(zhǎng)度: 6

默認(rèn)的構(gòu)造函數(shù)沒有任何參數(shù)。但是如果你需要一個(gè)帶有參數(shù)的構(gòu)造函數(shù)可以有參數(shù),這種構(gòu)造函數(shù)叫做參數(shù)化構(gòu)造函數(shù)。這種技術(shù)可以幫助你在創(chuàng)建對(duì)象的同時(shí)給對(duì)象賦初始值,具體請(qǐng)看下面范例:

using system;
namespace lineapplication
{
? ?class line
? ?{
? ? ? private double length; ? // 線條的長(zhǎng)度
? ? ? public line(double len) ?// 參數(shù)化構(gòu)造函數(shù)
? ? ? {
? ? ? ? ?console.writeline("對(duì)象已創(chuàng)建,length = {0}", len);
? ? ? ? ?length = len;
? ? ? }

? ? ? public void setlength( double len )
? ? ? {
? ? ? ? ?length = len;
? ? ? }
? ? ? public double getlength()
? ? ? {
? ? ? ? ?return length;
? ? ? }

? ? ? static void main(string[] args)
? ? ? {
? ? ? ? ?line line = new line(10.0);
? ? ? ? ?console.writeline("線條的長(zhǎng)度: {0}", line.getlength()); 
? ? ? ? ?// 設(shè)置線條長(zhǎng)度
? ? ? ? ?line.setlength(6.0);
? ? ? ? ?console.writeline("線條的長(zhǎng)度: {0}", line.getlength()); 
? ? ? ? ?console.readkey();
? ? ? }
? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

對(duì)象已創(chuàng)建,length = 10
線條的長(zhǎng)度: 10
線條的長(zhǎng)度: 6

 

4. c# 中的析構(gòu)函數(shù)

類的 析構(gòu)函數(shù) 是類的一個(gè)特殊的成員函數(shù),當(dāng)類的對(duì)象超出范圍時(shí)執(zhí)行。

析構(gòu)函數(shù)的名稱是在類的名稱前加上一個(gè)波浪形(~)作為前綴,它不返回值,也不帶任何參數(shù)。

析構(gòu)函數(shù)用于在結(jié)束程序(比如關(guān)閉文件、釋放內(nèi)存等)之前釋放資源。析構(gòu)函數(shù)不能繼承或重載。

下面的范例說(shuō)明了析構(gòu)函數(shù)的概念:

using system;
namespace lineapplication
{
? ?class line
? ?{
? ? ? private double length; ? // 線條的長(zhǎng)度
? ? ? public line() ?// 構(gòu)造函數(shù)
? ? ? {
? ? ? ? ?console.writeline("對(duì)象已創(chuàng)建");
? ? ? }
? ? ? ~line() //析構(gòu)函數(shù)
? ? ? {
? ? ? ? ?console.writeline("對(duì)象已刪除");
? ? ? }

? ? ? public void setlength( double len )
? ? ? {
? ? ? ? ?length = len;
? ? ? }
? ? ? public double getlength()
? ? ? {
? ? ? ? ?return length;
? ? ? }

? ? ? static void main(string[] args)
? ? ? {
? ? ? ? ?line line = new line();
? ? ? ? ?// 設(shè)置線條長(zhǎng)度
? ? ? ? ?line.setlength(6.0);
? ? ? ? ?console.writeline("線條的長(zhǎng)度: {0}", line.getlength()); ? ? ? ? ? 
? ? ? }
? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

對(duì)象已創(chuàng)建
線條的長(zhǎng)度: 6
對(duì)象已刪除

 

5. c# 類的靜態(tài)成員

我們可以使用 static 關(guān)鍵字把類成員定義為靜態(tài)的。當(dāng)我們聲明一個(gè)類成員為靜態(tài)時(shí),意味著無(wú)論有多少個(gè)類的對(duì)象被創(chuàng)建,只會(huì)有一個(gè)該靜態(tài)成員的副本。

關(guān)鍵字 static 意味著類中只有一個(gè)該成員的范例。靜態(tài)變量用于定義常量,因?yàn)樗鼈兊闹悼梢酝ㄟ^(guò)直接調(diào)用類而不需要?jiǎng)?chuàng)建類的范例來(lái)獲取。靜態(tài)變量可在成員函數(shù)或類的定義外部進(jìn)行初始化。你也可以在類的定義內(nèi)部初始化靜態(tài)變量。

下面的范例演示了靜態(tài)變量的用法:

using system;
namespace staticvarapplication
{
? ? class staticvar
? ? {
? ? ? ?public static int num;
? ? ? ? public void count()
? ? ? ? {
? ? ? ? ? ? num++;
? ? ? ? }
? ? ? ? public int getnum()
? ? ? ? {
? ? ? ? ? ? return num;
? ? ? ? }
? ? }
? ? class statictester
? ? {
? ? ? ? static void main(string[] args)
? ? ? ? {
? ? ? ? ? ? staticvar s1 = new staticvar();
? ? ? ? ? ? staticvar s2 = new staticvar();
? ? ? ? ? ? s1.count();
? ? ? ? ? ? s1.count();
? ? ? ? ? ? s1.count();
? ? ? ? ? ? s2.count();
? ? ? ? ? ? s2.count();
? ? ? ? ? ? s2.count(); ? ? ? ? 
? ? ? ? ? ? console.writeline("s1 的變量 num: {0}", s1.getnum());
? ? ? ? ? ? console.writeline("s2 的變量 num: {0}", s2.getnum());
? ? ? ? ? ? console.readkey();
? ? ? ? }
? ? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

s1 的變量 num: 6
s2 的變量 num: 6

你也可以把一個(gè)成員函數(shù)聲明為 static。這樣的函數(shù)只能訪問(wèn)靜態(tài)變量。靜態(tài)函數(shù)在對(duì)象被創(chuàng)建之前就已經(jīng)存在。下面的范例演示了靜態(tài)函數(shù)的用法:

using system;
namespace staticvarapplication
{
? ? class staticvar
? ? {
? ? ? ?public static int num;
? ? ? ? public void count()
? ? ? ? {
? ? ? ? ? ? num++;
? ? ? ? }
? ? ? ? public static int getnum()
? ? ? ? {
? ? ? ? ? ? return num;
? ? ? ? }
? ? }
? ? class statictester
? ? {
? ? ? ? static void main(string[] args)
? ? ? ? {
? ? ? ? ? ? staticvar s = new staticvar();
? ? ? ? ? ? s.count();
? ? ? ? ? ? s.count();
? ? ? ? ? ? s.count(); ? ? ? ? ? ? ? ? ? 
? ? ? ? ? ? console.writeline("變量 num: {0}", staticvar.getnum());
? ? ? ? ? ? console.readkey();
? ? ? ? }
? ? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

變量 num: 3

下一節(jié):c# 繼承

c# 教程

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频