- C# 基礎教程
- C# - 首頁
- C# - 概述
- C# - 環境
- C# - 程式結構
- C# - 基本語法
- C# - 資料型別
- C# - 型別轉換
- C# - 變數
- C# - 常量
- C# - 運算子
- C# - 決策
- C# - 迴圈
- C# - 封裝
- C# - 方法
- C# - 可空型別
- C# - 陣列
- C# - 字串
- C# - 結構體
- C# - 列舉
- C# - 類
- C# - 繼承
- C# - 多型
- C# - 運算子過載
- C# - 介面
- C# - 名稱空間
- C# - 預處理器指令
- C# - 正則表示式
- C# - 異常處理
- C# - 檔案 I/O
C# - 類
當您定義一個類時,您定義了一個數據型別的藍圖。這實際上並沒有定義任何資料,但它確實定義了類名的含義。也就是說,類的物件由什麼組成以及可以在該物件上執行什麼操作。物件是類的例項。構成類的成員函式和變數稱為類的成員。
定義類
類定義以關鍵字 class 後跟類名開始;以及由一對花括號括起來的類體。以下是類定義的一般形式:
<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
}
}
注意:
訪問修飾符指定成員以及類本身的訪問規則。如果未提及,則類型別的預設訪問修飾符為internal。成員的預設訪問許可權為private。
資料型別指定變數的型別,返回值型別指定方法返回的資料型別(如果有)。
要訪問類成員,請使用點 (.) 運算子。
點運算子將物件的名稱與成員的名稱連結起來。
以下示例說明了迄今為止討論的概念:
using System;
namespace BoxApplication {
class Box {
public double length; // Length of a box
public double breadth; // Breadth of a box
public double height; // Height of a box
}
class Boxtester {
static void Main(string[] args) {
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Volume of Box1 : 210 Volume of Box2 : 1560
成員函式和封裝
類的成員函式是在類定義中定義或其原型與任何其他變數類似的函式。它對它是成員的類的任何物件進行操作,並且可以訪問該物件的類的所有成員。
成員變數是物件(從設計角度)的屬性,並且它們被保密以實現封裝。這些變數只能使用公共成員函式訪問。
讓我們將上述概念應用於設定和獲取類中不同類成員的值:
using System;
namespace BoxApplication {
class Box {
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box
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(); // Declare Box1 of type Box
Box Box2 = new Box();
double volume;
// Declare Box2 of type Box
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}" ,volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Volume of Box1 : 210 Volume of Box2 : 1560
C# 建構函式
類的建構函式是類的特殊成員函式,每當我們建立該類的新的物件時都會執行。
建構函式的名稱與類完全相同,並且它沒有任何返回型別。以下示例解釋了建構函式的概念:
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() {
Console.WriteLine("Object is being created");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Object is being created Length of line : 6
預設建構函式沒有任何引數,但如果需要,建構函式可以有引數。此類建構函式稱為引數化建構函式。此技術可幫助您在建立物件時為其分配初始值,如下例所示:
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line(double len) { //Parameterized constructor
Console.WriteLine("Object is being created, 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("Length of line : {0}", line.getLength());
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Object is being created, length = 10 Length of line : 10 Length of line : 6
C# 解構函式
解構函式是類的特殊成員函式,每當其類的物件超出範圍時都會執行。解構函式的名稱與類完全相同,前面帶有字首波浪號 (~),它既不能返回值也不能接受任何引數。
解構函式對於在退出程式之前釋放記憶體資源非常有用。解構函式不能被繼承或過載。
以下示例解釋了解構函式的概念:
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() { // constructor
Console.WriteLine("Object is being created");
}
~Line() { //destructor
Console.WriteLine("Object is being deleted");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Object is being created Length of line : 6 Object is being deleted
C# 類中的靜態成員
我們可以使用static關鍵字將類成員定義為靜態。當我們將類的成員宣告為靜態時,這意味著無論建立多少個類的物件,靜態成員都只有一份副本。
關鍵字static意味著類只有一個成員例項。靜態變數用於定義常量,因為可以透過呼叫類而不建立其例項來檢索其值。靜態變數可以在成員函式或類定義之外進行初始化。您也可以在類定義內部初始化靜態變數。
以下示例演示了靜態變數的使用:
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("Variable num for s1: {0}", s1.getNum());
Console.WriteLine("Variable num for s2: {0}", s2.getNum());
Console.ReadKey();
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Variable num for s1: 6 Variable num for s2: 6
您還可以將成員函式宣告為靜態。此類函式只能訪問靜態變數。靜態函式甚至在建立物件之前就存在。以下示例演示了靜態函式的使用:
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("Variable num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
當編譯並執行上述程式碼時,會產生以下結果:
Variable num: 3