第三章知识点归纳
1.對象初始化器
?? 在調(diào)用構(gòu)造函數(shù)時直接初始化對象。
?
public class Student {public string Name;public int Age;public bool Gender; }Student stu=new Student{Name="zhangsan",Age=10,Gender=false};??? 等同于:
Student stu=new Student(); stu.Name="zhangsan"; stu.Age=10; stu.Gender=false;
2.this 和 base
?? this 是引用類實(shí)例自身。
?
public class Student {string name;public Test(string name){this.name=name;} }
base作用 : 1. 從子類訪問重載的基類方法成員 2.調(diào)用基類的構(gòu)造方法
??引用書上的例子:
public class Asset {public string name;public virtual decimal Liability{ get { return 0; } } }public class Home : Asset {public decimal Mortgage;public override decimal Liability{get { return base.Liability + Mortgage; }} }
3.裝箱和拆箱
?? 裝箱是將值類型轉(zhuǎn)換為引用類型。
?? int num=10;
?? object obj = x; //把int類型裝箱
? 拆箱是講引用類型轉(zhuǎn)換為值類型。
? object obj = 10;
? int num= (int)obj;
? 裝箱和拆箱的實(shí)質(zhì)是復(fù)制:?裝箱是把值類型的實(shí)例復(fù)制到新對象中,拆箱是把對象的內(nèi)容復(fù)制回?cái)?shù)值類型的實(shí)例中。
?
?4. 協(xié)變和逆變
??? 協(xié)變:假定A是B的子類,如果C<A>可以引用轉(zhuǎn)化成C<B>,那么稱C為協(xié)變類。
??? IAbc<string> a = ... ;
??? IAbc<object> b = a;
??? IAbc<T> 是協(xié)變類。
??? 逆變:假定A是B的子類,如果C<B>可以引用轉(zhuǎn)化成C<A>,那么稱C為逆變類。
轉(zhuǎn)載于:https://www.cnblogs.com/wingswang/p/4571267.html
總結(jié)
- 上一篇: 资料收集新一代 Linux 文件系统 b
- 下一篇: 悼念传奇,约翰询问#183;纳什和他的妻