《C#入門詳解》劉老師 字段&屬性&索引器&常量&類的實例
字段&屬性&索引器&常量&類的實例
字段
-
什么是字段
1.字段是一種表示與對象或類型(類與結構提)關聯的變量
2.字段是類型的成員
3.與對象關聯的字段稱“實例字段”
4.與類型關聯的字段稱為“靜態字段”,由static修飾,表示這個類型當前的狀態
例子:
console.writeline(student.Amount);
class student{public int age=20;public static int amount; static student(){student.amount=100;} }
-
字段的聲明
-
字段的初始化
1.無顯式初始化時,字段獲得其類型的默認值,所以字段“永遠都不會未被初始化”
2.實例字段初始化的時機--------對象創建時
3.靜態字段初始化的時機--------類型被加載時
-
只讀字段
1.實例只讀字段
2.靜態只讀字段
PS:靜態構造器(static 類名)是在數據類型被運行環境加載的時候執行,而且只執行一次
class program { static void main(string[] args) { console.writeline(Brush.DefaultColor.red); console.writeline(Brush.DefaultColor.green); /*Brush.DefaultColor = new Color(){red=255,Green=255};靜態只讀字段不能被賦值,只能進行初始化。靜態只讀字段初始化有兩種方法,第一種靜態構造器中初始化,第二種在聲明的時候后面跟上初始化器*/ } struct color { public int Red; public int Green; public int Blue; } class Brush { public static readonly Color DefaultColor = new Color(){red=0,green=0};//A /*==========聲明了一個靜態只讀字段,并對它賦值。=================聲明后對字段初始化======================================================*/
static Brush()//靜態構造函數 //B { Brush.DefaultColor = new Color(){red=0,Green=0}; } /*A和B是一樣的 } }
屬性
-
什么是屬性
1.屬性是一種用于訪問對象或類型的特征的成員,特征反映了狀態
2.屬性是字段的自然擴展
從命名上看,field更偏向于實例對象在內存中的布局,property更偏向于反映現實世界對象的特征
對外:暴露數據,數據可以存儲在字段里,也可以動態計算出
對內:保護字段不被非法值“污染”
3.屬性是由get/set方法對進化來
-
屬性的聲明
1.完整聲明-------后臺成員變量與訪問器
2.簡略聲明-------只有訪問器
3.注意實例屬性和靜態屬性
4.只讀屬性,只有getter沒有setter
-
屬性與字段的關系
1.一般情況下,它們都用于表示實體(對象或類型的)狀態
2.屬性大多數情況下是字段的包裝器
3.建議永遠使用屬性(而不是字段)來暴露數據,即字段永遠都是private
using system; namespace Property_Example { class program { static void main(string[] args) { student stu1 = new student(); stu1.setage(20); student stu2 = new student(); stu2.setage(20); student stu3 = new student(); stu3.setage(20); int avgage = (stu1.getage()+stu2.getage()+stu3.getage())/3; } } class student { private int age; public int Getage() { return this.age; } public void setage(int value) { if (value>=0&&value<=120){this.age=value;} else{ throw new Exception("age value is error");} } } } /*-------------------------屬性--------------------------------------*/ using system; namespace Property_Example { class program { static void main(string[] args) { student stu1 = new student(); stu1.age(20); student stu2 = new student(); stu2.age(20); student stu3 = new student(); stu3.age(20); int avgage = (stu1.age()+stu2.age()+stu3.age())/3; } } class student { private int age; public int age { get { return this.age; } set { if (value>=0&&value<=120){this.age=value;} else { throw new exception("age value has error");} } } }
using system; namespace hello { class program { static void main(string[] args) { student stu1 = new student(){id=1,name="a"};//用初始化器初始化一下 console.writeline(stu.id); //stu.id=1 } } class student { public int age{get;set;}; public string name{get;set;} } }
/*====================================================區別一======================================================================*/
using system;
namespace hello
{
class program
{
static void main(string[] args)
{
student stu1 = new student(); //默認構造器對這邊的實例對象的字段進行初始化
console.writeline(stu.id); //stu.id=0
}
}
class student
{
public int age{get;set;};
public string name{get;set;}
}
}
/*===================================================區別二實例化和初始化合并=============================================================*/
static void main(string[] args)
{
student stu = new student (2,"MR.OKAY");//傳參數滿足自定義構造器的需求
console.writeline(stu.id);
console.writeline(stu.name);
}
class student
{
public int id;
public string name;
public student ( int initid , string initname)
{ this.id = intitid;
this.name= initname;
}
}
=========================================================參RP基礎知識復習=======================================================================

集合初始化器:
用值來初始化數組:
int[] Array = new int[5] { 3, 11, 18, 22, 34 };
這是一種合并實例化和初始化數組的簡捷方式。集合初始化器只是把這個語法擴展到集合上:
List<int> Collection = new List<int> { 8, 25, 33, 24, 20 };
通過合并對象和集合初始化器,就可以用簡潔的代碼配置集合了。下面的代碼:
1 List<Student> student = new List<Student>(); 2 3 curries.Add(new Student("xiaoming", "1300108", 22)); 4 5 curries.Add(new Student("xiaohong", "1300109", 23)); 6 7 curries.Add(new Student("xiaozhang", "1300110", 22));
可以用如下代碼替換:
1 List<Student> student = new List<Student> 2 3 { 4 5 new Student1 6 7 {Name = "xiaoming",ID = "1300108",Age=22}, 8 9 10 new Student2 11 12 {Name = "xiaohong",ID = "1300109",Age=22}, 13 14 new Student3 15 16 {Name = "xiaozhang",ID = "1300110",Age=22}, 17 18 };
實例化
上面有初始化,這邊主要是實例化發生的事情。
實例化一個類的時候,先是里面有賦值的字段或屬性,然后再是構造函數,里面未賦值的字段和屬性不會運行。當我們實例好之后,可以用那個未賦值的字段和屬性。
類A繼承自類C,類A實例化時,類A ctor開始時,類C實現類中已經賦值的字段或屬性,然后類C開始ctor


浙公網安備 33010602011771號