結(jié)構(gòu)體
結(jié)構(gòu)體定義
結(jié)構(gòu)體的語(yǔ)法格式:
?struct + 結(jié)構(gòu)體名
?{
?結(jié)構(gòu)體成員變量(相當(dāng)于類中的字段)
?}
?結(jié)構(gòu)體中的特點(diǎn):
?1.結(jié)構(gòu)體不能被繼承
?2.結(jié)構(gòu)體除了可以擁有結(jié)構(gòu)體成員變量外艺栈,還可以有屬性
?3.結(jié)構(gòu)體和類一樣英岭,也可以有自己的行為(也就是方法)
?4.結(jié)構(gòu)體如果內(nèi)部不存在任何構(gòu)造函數(shù),此時(shí)系統(tǒng)會(huì)像類那樣為結(jié)構(gòu)自動(dòng)添加無參數(shù)的構(gòu)造函數(shù)
?5.結(jié)構(gòu)體內(nèi)部如果存在構(gòu)造函數(shù)湿右,此時(shí)必須對(duì)結(jié)構(gòu)體內(nèi)部所有變量賦值
?6.如果結(jié)構(gòu)體內(nèi)部存在多個(gè)構(gòu)造函數(shù)诅妹,不管你創(chuàng)建結(jié)構(gòu)體變量的時(shí)候有沒有調(diào)用構(gòu)造函數(shù),都必須對(duì)所有
?7.C#4.0之前系統(tǒng)不支持手動(dòng)調(diào)用結(jié)構(gòu)體無參構(gòu)造函數(shù)——寫都不能寫
? ? ?C#5.0支持手動(dòng)寫默認(rèn)構(gòu)造函數(shù)
?8.結(jié)構(gòu)體中不管你是否寫了帶有參數(shù)的構(gòu)造函數(shù)毅人,系統(tǒng)仍然會(huì)提供一個(gè)無參數(shù)的構(gòu)造函數(shù)
?9.結(jié)構(gòu)體中的new不是開辟空間吭狡,既然要開辟空間必然有性能上的損耗,因?yàn)橥泄芏眩ǘ眩┬枰獌?yōu)化
?10.結(jié)構(gòu)體損耗性能比較小丈莺,如果你設(shè)計(jì)的數(shù)據(jù)結(jié)構(gòu)是小型輕量級(jí)的划煮,這時(shí)候可以采用結(jié)構(gòu)體儲(chǔ)存
?11.結(jié)構(gòu)體可以指定結(jié)構(gòu)體成員在內(nèi)存的分布
?12.結(jié)構(gòu)體中不能有析構(gòu)函數(shù)
struct Student //定義結(jié)構(gòu)體,不能繼承
{
public string name;//定義結(jié)構(gòu)體中的字段
public int age;
public int sno;
public int score;
public Student(string name,int age,int sno,int score)//結(jié)構(gòu)體的構(gòu)造方法缔俄,必須初始化所有對(duì)象
{
this.name = name;
this.age = age;
this.sno = sno;
this.score = score;
Console.WriteLine ("name ={0}? age ={1}? sno ={2}? score ={3}",name,age,sno,score);
}
class MainClass
{
public static void Main (string[] args)
{
//實(shí)例化五個(gè)對(duì)象弛秋,也可不用new,這里需要調(diào)用構(gòu)造方法
Student s = new Student ("a1", 20, 4, 50);
Student s1 = new Student ("a2", 21, 3, 30);
Student s2 = new Student ("a3", 38, 2, 20);
Student s3 = new Student ("a4", 15, 5, 60);
Student s4 = new Student ("a5", 30, 1, 90);
//找出最高值
int index = 0;
int max = 0;
Student[] array = new Student[]{ s, s1, s2, s3, s4 };//將五個(gè)對(duì)象存入結(jié)構(gòu)體數(shù)組中
for (int i = 0; i < 5; i++) {
if (array [i].score > max) {
max = array [i].score;
index = i;//記錄最大值的下標(biāo)
}
}
Console.WriteLine ("分?jǐn)?shù)最高的是{0}俐载,分?jǐn)?shù)是{1}蟹略,年齡是{2},學(xué)號(hào){3}",array[index].name,array[index].score,array[index].age,array[index].sno);
//按年齡排序
//利用冒泡排序?qū)?shù)組中結(jié)構(gòu)體位置交換
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5-i-1; j++) {
if (array [j].age > array [j + 1].age) {
Student temp = array [j];
array [j] = array [j + 1];
array [j + 1] = temp;
}
}
}
Console.WriteLine ("按年齡排序是");
foreach (var item in array) {
Console.WriteLine ("名字 ={0}? 年齡 ={1}? 學(xué)號(hào) ={2}? 分?jǐn)?shù) ={3}",item.name,item.age,item.sno,item.score);
}
}
}
??
?析構(gòu)方法
?析構(gòu)方法~+類名
?析構(gòu)方法沒有返回值遏佣,沒有參數(shù)
?作用:銷毀對(duì)象的
?析構(gòu)方法不能手動(dòng)進(jìn)行調(diào)用挖炬,由系統(tǒng)自動(dòng)調(diào)用
?在實(shí)際開發(fā)中,析構(gòu)函數(shù)的作用就是斷開socket連接或者斷開數(shù)據(jù)庫(kù)
?
internal是修飾類的默認(rèn)修飾符状婶。只有當(dāng)前程序集才可以訪問
嘗試橫跨程序集進(jìn)行訪問:
?目標(biāo):訪問Test程序集MainClass類中的Show方法
?步驟:
?1.編譯你想訪問的那個(gè)程序集 -->編譯后的程序文件存于系統(tǒng)的某個(gè)位置
?2.在訪問程序集中的Reference文件夾中意敛,右鍵選中編輯媒吗,然后彈出的界面選中Project欄鸠按,此時(shí)你會(huì)看到你想訪問的那個(gè)程序集,勾選中點(diǎn)擊ok
?3.在訪問程序集中MainClass函數(shù)中調(diào)用肺孵,調(diào)用的格式為走敌,命名空間.類名