base重要用于OOP的多態(tài)上,base 關(guān)鍵字用于在派生類(lèi)中實(shí)現對基類(lèi)公有或者受保護成員的訪(fǎng)問(wèn),但是只局限在構造函數、實(shí)例方法和實(shí)例屬性訪(fǎng)問(wèn)器中
using System;
namespace BaseDemo{ class Program { static void Main(string[] args) { B b = new B(); Console.ReadLine(); } } public class A { public A() { Console.WriteLine("Build A"); } } public class B : A { public B() : base() { Console.WriteLine("Build B"); } }}執行結果:
using System;
namespace BaseDemo{ class Program { static void Main(string[] args) { B b = new B(); b.Hello(); Console.ReadLine(); } } public class A { public virtual void Hello() { Console.WriteLine("Hello"); } } public class B : A { public override void Hello() { base.Hello(); } }}執行結果:
using System;
namespace thisDemo{ class Program { static void Main(string[] args) { A a = new A(); a.GetName(); Console.ReadKey(); } } public class A { private string name = "qxh"; public void GetName() { string name = "歡迎你"; // this代表A的實(shí)例對象 // 所以this.name對應的是"qxh" // name對應的是GetName方法內的局部變量 Console.WriteLine($" {this.name} - {name}"); } }}執行結果:
using System;
namespace thisDemo{ class Program { static void Main(string[] args) { A a = new A("qxh"); Console.ReadKey(); } } public class A { public A() { Console.WriteLine("無(wú)參構造函數"); } // this()對應無(wú)參構造方法A() // 先執行A(),后執行A(string text) public A(string text) : this() { Console.WriteLine(text); Console.WriteLine("有參構造函數"); } }}
執行結果:
using System;
namespace thisDemo{ class Program { static void Main(string[] args) { string name = "qxh"; Console.WriteLine(name.ShowInfo("歡迎你"));
Console.ReadKey(); } } /// <summary> /// 必須是靜態(tài)類(lèi) /// </summary> public static class A { public static string ShowInfo(this string str, string info) { return $"{str}-{info}"; } }}
執行結果:
using System;
namespace thisDemo{ class Program { static void Main(string[] args) {
A a = new A(); Console.WriteLine(a["qxh"]); Console.ReadKey(); } } public class A { public string Name { get; set; } //索引器 []括號里面可以是string,int,array等 public string this[string index] { get { if (index == "qxh") { return index; } else { return "gg"; } } set { Name = value; } } }}
執行結果:

new一個(gè)class時(shí),new完成了以下兩個(gè)方面的內容:一是調用new class命令來(lái)為實(shí)例在托管堆中分配內存;二是調用構造函數來(lái)實(shí)現對象初始化。
new一個(gè)struct時(shí),new運算符用于調用其帶構造函數,完成實(shí)例的初始化。
new一個(gè)int時(shí),new運算符用于初始化其值為0。
new運算符不可重載。
new分配內存失敗,將引發(fā)OutOfMemoryException異常。
static void Main(string[] args){ A a = new A();//new操作符 創(chuàng )建對象和調用構造函數 Console.ReadKey();}new 關(guān)鍵字可以顯式隱藏從基類(lèi)繼承的成員。 隱藏繼承的成員時(shí),該成員的派生版本將替換基類(lèi)版本。 雖然可以在不使用 new 修飾符的情況下隱藏成員,但會(huì )生成警告。 如果使用 new 顯式隱藏成員,則會(huì )取消此警告,并記錄要替換為派生版本這一事實(shí)。下面是隱藏 A中的Name屬性
public class A{ public string Name { get; set; } = "qxh";}
public class B : A{ new public string Name { get; set; } = "dachong";}
new 約束指定泛型類(lèi)聲明中的任何類(lèi)型參數都必須有公共的無(wú)參數構造函數。如果要使用 new 約束,則該類(lèi)型不能為抽象類(lèi)型。當與其他約束一起使用時(shí),new() 約束必須最后指定:
public class A<T>where T : IComparable, new(){ ////}要擴展或修改繼承的方法、屬性、索引器或事件的抽象實(shí)現或虛實(shí)現,必須使用 override 修飾符。 由 override 聲明重寫(xiě)的方法稱(chēng)為重寫(xiě)基方法。重寫(xiě)的基方法必須與 override 方法具有相同的簽名。不能重寫(xiě)非虛方法或靜態(tài)方法。 重寫(xiě)的基方法必須是 virtual、abstract 或 override 的。用關(guān)鍵字 virtual 修飾的方法,叫虛方法??梢栽谧宇?lèi)中用override 聲明同名的方法,這叫“重寫(xiě)”。相應的沒(méi)有用virtual修飾的方法,我們叫它實(shí)方法。
using System;
namespace thisDemo{ class Program { static void Main(string[] args) { B b = new B(); b.Show(); Console.ReadKey(); } } public class A { public virtual void Show() { Console.WriteLine("A"); } }
public class B : A { public override void Show() { Console.WriteLine("B"); } }}
執行結果:

用關(guān)鍵字abstract定義的類(lèi)即為抽象類(lèi),且只能作為基類(lèi),也不能被實(shí)例化。
用abstract定義的類(lèi)不一定包含抽象方法,也可以包含非抽象方法。
abstract定義的方法一定包含在抽象類(lèi)中。
抽象類(lèi)不能定義為密封類(lèi)(sealed),抽象方法不能使用virtual、static、private修飾符
如果派生類(lèi)沒(méi)有實(shí)現所有的抽象方法,則該派生類(lèi)也必須聲明為抽象類(lèi)。
聯(lián)系客服