C# 类
当你定义一个类时,你定义了一个数据类型的蓝图。这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作。对象是类的实例。构成类的方法和变量成为类的成员。
一、类的定义
类的定义是以关键字 class
开始,后跟类的名称。类的主体,包含在一对花括号内。下面是类定义的一般形式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<access specifier> <span class="hljs-keyword">class</span> <span class="hljs-title">class_name</span> { <span class="hljs-comment">// member variables</span> <access specifier> <data type> variable1; <access specifier> <data type> variable2; ... <access specifier> <data type> variableN; <span class="hljs-comment">// member methods</span> <access specifier> <<span class="hljs-keyword">return</span> type> method1(parameter_list) { <span class="hljs-comment">// method body </span> } <access specifier> <<span class="hljs-keyword">return</span> type> method2(parameter_list) { <span class="hljs-comment">// method body </span> } ... <access specifier> <<span class="hljs-keyword">return</span> type> methodN(parameter_list) { <span class="hljs-comment">// method body </span> } } |
请注意:
- 访问标识符 指定了对类及其成员的访问规则。如果没有指定,则使用默认的访问标识符。类的默认访问标识符是 internal,成员的默认访问标识符是 private。
- 数据类型 指定了变量的类型,返回类型 指定了返回的方法返回的数据类型。
- 如果要访问类的成员,你要使用点(.)运算符。
- 点运算符链接了对象的名称和成员的名称。
下面的实例说明了目前为止所讨论的概念:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">BoxApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">Box</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> length; <span class="hljs-comment">// 长度</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> breadth; <span class="hljs-comment">// 宽度</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> height; <span class="hljs-comment">// 高度</span> } <span class="hljs-keyword">class</span> <span class="hljs-title">Boxtester</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ Box Box1 = <span class="hljs-keyword">new</span> Box(); <span class="hljs-comment">// 声明 Box1,类型为 Box</span> Box Box2 = <span class="hljs-keyword">new</span> Box(); <span class="hljs-comment">// 声明 Box2,类型为 Box</span> <span class="hljs-keyword">double</span> volume = <span class="hljs-number">0.0</span>; <span class="hljs-comment">// 体积</span> <span class="hljs-comment">// Box1 详述</span> Box1.height = <span class="hljs-number">5.0</span>; Box1.length = <span class="hljs-number">6.0</span>; Box1.breadth = <span class="hljs-number">7.0</span>; <span class="hljs-comment">// Box2 详述</span> Box2.height = <span class="hljs-number">10.0</span>; Box2.length = <span class="hljs-number">12.0</span>; Box2.breadth = <span class="hljs-number">13.0</span>; <span class="hljs-comment">// Box1 的体积</span> volume = Box1.height * Box1.length * Box1.breadth; Console.WriteLine(<span class="hljs-string">"Box1 的体积: {0}"</span>, volume); <span class="hljs-comment">// Box2 的体积</span> volume = Box2.height * Box2.length * Box2.breadth; Console.WriteLine(<span class="hljs-string">"Box2 的体积: {0}"</span>, volume); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
Box1 的体积: <span class="hljs-number">210</span> Box2 的体积: <span class="hljs-number">1560</span> |
二、成员函数和封装
类的成员函数是一个在类定义中有它的定义或原型的函数,就像其他变量一样。作为类的一个成员,它能在类的任何对象上操作,且能访问该对象的类的所有成员。
成员变量是对象的属性(从设计角度),且它们保持私有来实现封装。这些变量只能使用公共成员函数来访问。
让我们使用上面的概念来设置和获取一个类中不同的类成员的值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">BoxApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">Box</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> length; <span class="hljs-comment">// 长度</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> breadth; <span class="hljs-comment">// 宽度</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> height; <span class="hljs-comment">// 高度</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setLength</span>(<span class="hljs-params"> <span class="hljs-keyword">double</span> len </span>) </span>{ length = len; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setBreadth</span>(<span class="hljs-params"> <span class="hljs-keyword">double</span> bre </span>) </span>{ breadth = bre; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setHeight</span>(<span class="hljs-params"> <span class="hljs-keyword">double</span> hei </span>) </span>{ height = hei; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">getVolume</span>() </span>{ <span class="hljs-keyword">return</span> length * breadth * height; } } <span class="hljs-keyword">class</span> <span class="hljs-title">Boxtester</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ Box Box1 = <span class="hljs-keyword">new</span> Box(); <span class="hljs-comment">// 声明 Box1,类型为 Box</span> Box Box2 = <span class="hljs-keyword">new</span> Box(); <span class="hljs-comment">// 声明 Box2,类型为 Box</span> <span class="hljs-keyword">double</span> volume; <span class="hljs-comment">// 体积</span> <span class="hljs-comment">// Box1 详述</span> Box1.setLength(<span class="hljs-number">6.0</span>); Box1.setBreadth(<span class="hljs-number">7.0</span>); Box1.setHeight(<span class="hljs-number">5.0</span>); <span class="hljs-comment">// Box2 详述</span> Box2.setLength(<span class="hljs-number">12.0</span>); Box2.setBreadth(<span class="hljs-number">13.0</span>); Box2.setHeight(<span class="hljs-number">10.0</span>); <span class="hljs-comment">// Box1 的体积</span> volume = Box1.getVolume(); Console.WriteLine(<span class="hljs-string">"Box1 的体积: {0}"</span> ,volume); <span class="hljs-comment">// Box2 的体积</span> volume = Box2.getVolume(); Console.WriteLine(<span class="hljs-string">"Box2 的体积: {0}"</span>, volume); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
Box1 的体积: <span class="hljs-number">210</span> Box2 的体积: <span class="hljs-number">1560</span> |
三、C# 中的构造函数
类的 构造函数 是类的一个特殊的成员函数,当创建类的新对象时执行。
构造函数的名称与类的名称完全相同,它没有任何返回类型。
下面的实例说明了构造函数的概念:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">LineApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">Line</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> length; <span class="hljs-comment">// 线条的长度</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Line</span>() </span>{ Console.WriteLine(<span class="hljs-string">"对象已创建"</span>); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setLength</span>(<span class="hljs-params"> <span class="hljs-keyword">double</span> len </span>) </span>{ length = len; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">getLength</span>() </span>{ <span class="hljs-keyword">return</span> length; } <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ Line line = <span class="hljs-keyword">new</span> Line(); <span class="hljs-comment">// 设置线条长度</span> line.setLength(<span class="hljs-number">6.0</span>); Console.WriteLine(<span class="hljs-string">"线条的长度: {0}"</span>, line.getLength()); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
对象已创建 线条的长度: <span class="hljs-number">6</span> |
默认的构造函数没有任何参数。但是如果你需要一个带有参数的构造函数可以有参数,这种构造函数叫做参数化构造函数。这种技术可以帮助你在创建对象的同时给对象赋初始值,具体请看下面实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">LineApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">Line</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> length; <span class="hljs-comment">// 线条的长度</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Line</span>(<span class="hljs-params"><span class="hljs-keyword">double</span> len</span>) <span class="hljs-comment">// 参数化构造函数</span> </span>{ Console.WriteLine(<span class="hljs-string">"对象已创建,length = {0}"</span>, len); length = len; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setLength</span>(<span class="hljs-params"> <span class="hljs-keyword">double</span> len </span>) </span>{ length = len; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">getLength</span>() </span>{ <span class="hljs-keyword">return</span> length; } <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ Line line = <span class="hljs-keyword">new</span> Line(<span class="hljs-number">10.0</span>); Console.WriteLine(<span class="hljs-string">"线条的长度: {0}"</span>, line.getLength()); <span class="hljs-comment">// 设置线条长度</span> line.setLength(<span class="hljs-number">6.0</span>); Console.WriteLine(<span class="hljs-string">"线条的长度: {0}"</span>, line.getLength()); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 4 |
对象已创建,length = <span class="hljs-number">10</span> 线条的长度: <span class="hljs-number">10</span> 线条的长度: <span class="hljs-number">6</span> |
四、C# 中的析构函数
类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行。
析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数。
析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源。析构函数不能继承或重载。
下面的实例说明了析构函数的概念:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">LineApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">Line</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> length; <span class="hljs-comment">// 线条的长度</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Line</span>() <span class="hljs-comment">// 构造函数</span> </span>{ Console.WriteLine(<span class="hljs-string">"对象已创建"</span>); } ~Line() <span class="hljs-comment">//析构函数</span> { Console.WriteLine(<span class="hljs-string">"对象已删除"</span>); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setLength</span>(<span class="hljs-params"> <span class="hljs-keyword">double</span> len </span>) </span>{ length = len; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> <span class="hljs-title">getLength</span>() </span>{ <span class="hljs-keyword">return</span> length; } <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ Line line = <span class="hljs-keyword">new</span> Line(); <span class="hljs-comment">// 设置线条长度</span> line.setLength(<span class="hljs-number">6.0</span>); Console.WriteLine(<span class="hljs-string">"线条的长度: {0}"</span>, line.getLength()); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 4 |
对象已创建 线条的长度: <span class="hljs-number">6</span> 对象已删除 |
五、C# 类的静态成员
我们可以使用 static 关键字把类成员定义为静态的。当我们声明一个类成员为静态时,意味着无论有多少个类的对象被创建,只会有一个该静态成员的副本。
关键字 static 意味着类中只有一个该成员的实例。静态变量用于定义常量,因为它们的值可以通过直接调用类而不需要创建类的实例来获取。静态变量可在成员函数或类的定义外部进行初始化。你也可以在类的定义内部初始化静态变量。
下面的实例演示了静态变量的用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">StaticVarApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">StaticVar</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> num; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">count</span>() </span>{ num++; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getNum</span>() </span>{ <span class="hljs-keyword">return</span> num; } } <span class="hljs-keyword">class</span> <span class="hljs-title">StaticTester</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ StaticVar s1 = <span class="hljs-keyword">new</span> StaticVar(); StaticVar s2 = <span class="hljs-keyword">new</span> StaticVar(); s1.count(); s1.count(); s1.count(); s2.count(); s2.count(); s2.count(); Console.WriteLine(<span class="hljs-string">"s1 的变量 num: {0}"</span>, s1.getNum()); Console.WriteLine(<span class="hljs-string">"s2 的变量 num: {0}"</span>, s2.getNum()); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
s1 的变量 num: <span class="hljs-number">6</span> s2 的变量 num: <span class="hljs-number">6</span> |
你也可以把一个成员函数声明为 static。这样的函数只能访问静态变量。静态函数在对象被创建之前就已经存在。下面的实例演示了静态函数的用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<span class="hljs-keyword">using</span> System; <span class="hljs-keyword">namespace</span> <span class="hljs-title">StaticVarApplication</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">StaticVar</span> { <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> num; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">count</span>() </span>{ num++; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getNum</span>() </span>{ <span class="hljs-keyword">return</span> num; } } <span class="hljs-keyword">class</span> <span class="hljs-title">StaticTester</span> { <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ StaticVar s = <span class="hljs-keyword">new</span> StaticVar(); s.count(); s.count(); s.count(); Console.WriteLine(<span class="hljs-string">"变量 num: {0}"</span>, StaticVar.getNum()); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 |
变量 num: <span class="hljs-number">3</span> |