當您定義一個類時,您定義了一個數(shù)據(jù)類型的藍圖。這實際上并沒有定義任何的數(shù)據(jù),但它定義了類的名稱意味著什么,也就是說,類的對象由什么組成及在這個對象上可執(zhí)行什么操作。對象是類的實例。構(gòu)成類的方法和變量成為類的成員。
類的定義是以關(guān)鍵字 class 開始,后跟類的名稱。類的主體,包含在一對花括號內(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 } }
請注意:
下面的實例說明了目前為止所討論的概念:
using System; namespace BoxApplication { class Box { public double length; // 長度 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(); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
Box1 的體積: 210 Box2 的體積: 1560
類的成員函數(shù)是一個在類定義中有它的定義或原型的函數(shù),就像其他變量一樣。作為類的一個成員,它能在類的任何對象上操作,且能訪問該對象的類的所有成員。
成員變量是對象的屬性(從設計角度),且它們保持私有來實現(xiàn)封裝。這些變量只能使用公共成員函數(shù)來訪問。
讓我們使用上面的概念來設置和獲取一個類中不同的類成員的值:
using System; namespace BoxApplication { class Box { private double length; // 長度 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(); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
Box1 的體積: 210 Box2 的體積: 1560
類的 構(gòu)造函數(shù) 是類的一個特殊的成員函數(shù),當創(chuàng)建類的新對象時執(zhí)行。
構(gòu)造函數(shù)的名稱與類的名稱完全相同,它沒有任何返回類型。
下面的實例說明了構(gòu)造函數(shù)的概念:
using System; namespace LineApplication { class Line { private double length; // 線條的長度 public Line() { Console.WriteLine("對象已創(chuàng)建"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // 設置線條長度 line.setLength(6.0); Console.WriteLine("線條的長度: {0}", line.getLength()); Console.ReadKey(); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
對象已創(chuàng)建 線條的長度: 6
默認的構(gòu)造函數(shù)沒有任何參數(shù)。但是如果您需要一個帶有參數(shù)的構(gòu)造函數(shù)可以有參數(shù),這種構(gòu)造函數(shù)叫做參數(shù)化構(gòu)造函數(shù)。這種技術(shù)可以幫助您在創(chuàng)建對象的同時給對象賦初始值,具體請看下面實例:
using System; namespace LineApplication { class Line { private double length; // 線條的長度 public Line(double len) // 參數(shù)化構(gòu)造函數(shù) { Console.WriteLine("對象已創(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("線條的長度: {0}", line.getLength()); // 設置線條長度 line.setLength(6.0); Console.WriteLine("線條的長度: {0}", line.getLength()); Console.ReadKey(); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
對象已創(chuàng)建,length = 10 線條的長度: 10 線條的長度: 6
類的 析構(gòu)函數(shù) 是類的一個特殊的成員函數(shù),當類的對象超出范圍時執(zhí)行。
析構(gòu)函數(shù)的名稱是在類的名稱前加上一個波浪形(~)作為前綴,它不返回值,也不帶任何參數(shù)。
析構(gòu)函數(shù)用于在結(jié)束程序(比如關(guān)閉文件、釋放內(nèi)存等)之前釋放資源。析構(gòu)函數(shù)不能繼承或重載。
下面的實例說明了析構(gòu)函數(shù)的概念:
using System; namespace LineApplication { class Line { private double length; // 線條的長度 public Line() // 構(gòu)造函數(shù) { Console.WriteLine("對象已創(chuàng)建"); } ~Line() //析構(gòu)函數(shù) { Console.WriteLine("對象已刪除"); } public void setLength( double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line = new Line(); // 設置線條長度 line.setLength(6.0); Console.WriteLine("線條的長度: {0}", line.getLength()); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
對象已創(chuàng)建 線條的長度: 6 對象已刪除
我們可以使用 static 關(guān)鍵字把類成員定義為靜態(tài)的。當我們聲明一個類成員為靜態(tài)時,意味著無論有多少個類的對象被創(chuàng)建,只會有一個該靜態(tài)成員的副本。
關(guān)鍵字 static 意味著類中只有一個該成員的實例。靜態(tài)變量用于定義常量,因為它們的值可以通過直接調(diào)用類而不需要創(chuàng)建類的實例來獲取。靜態(tài)變量可在成員函數(shù)或類的定義外部進行初始化。您也可以在類的定義內(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(); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
s1 的變量 num: 6 s2 的變量 num: 6
您也可以把一個成員函數(shù)聲明為 static。這樣的函數(shù)只能訪問靜態(tài)變量。靜態(tài)函數(shù)在對象被創(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(); } } }
當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:
變量 num: 3