《C#与.net高级编程》——第一支柱:C#的封装
This關(guān)鍵字的作用:
解決當(dāng)傳入?yún)?shù)的名字和類型數(shù)據(jù)字段的相同時(shí)產(chǎn)生的作用域歧義。
構(gòu)造函數(shù)鏈
1 class Motorcycle 2 { 3 public int driverInterIntensity; 4 public string driverName; 5 //構(gòu)造函數(shù)鏈 6 public Motorcycle(){} 7 public Motorcycle(int intensity) 8 : this(intensity,""){} 9 public Motorcycle(string name) 10 : this(0,name) {} 11 //這是做所有工作的“主”構(gòu)造函數(shù) 12 public Motorcycle(int intensity ,string name) 13 { 14 if (intonsity > 10) 15 { 16 intensity =10; 17 } 18 driverIntensity = intensity; 19 driverName = name; 20 } 21 }構(gòu)造函數(shù)鏈的鏈接流程
常量數(shù)據(jù)
關(guān)鍵字:const
注:定義常量時(shí)必須為常量指定初始值。
只讀字段
關(guān)鍵字:readonly
?? 和常量緊密聯(lián)系的概念是只讀字段數(shù)據(jù)。和常量相似,只讀字段不能在賦初始值后改變。然而,和常量不同的是,賦給只讀字段的值可以在運(yùn)行時(shí)決定,因此在構(gòu)造函數(shù)作用域中進(jìn)行賦值是合法的。
靜態(tài)只讀字段
關(guān)鍵字:static readonly
分部類型
使用分部類將構(gòu)造函數(shù)和字段數(shù)據(jù)轉(zhuǎn)移到全新的 Employee.Internal.cs 文件中。第一步是向當(dāng)前的類定義中添加partial關(guān)鍵字,再剪切轉(zhuǎn)移到新文件中的代碼:
1 // Employee.cs 2 partial class Employee 3 { 4 //方法 5 //屬性 6 }然后,假設(shè)已經(jīng)在項(xiàng)目中插入了新的類文件,將數(shù)據(jù)字段和構(gòu)造函數(shù)粘貼到新文件中。此外,還必須在類定義中添加partial關(guān)鍵字。例如:
1 // Employee.Internal.cs 2 partial class Employee 3 { //字段數(shù)據(jù) 4 //構(gòu)造函數(shù) 5 }轉(zhuǎn)載于:https://www.cnblogs.com/chenmoit/archive/2012/11/08/2759830.html
總結(jié)
以上是生活随笔為你收集整理的《C#与.net高级编程》——第一支柱:C#的封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle函数 INITCAP(c1)
- 下一篇: NET(C#):await返回Task的