欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
C#基礎知識之base、this、new、override、abstract梳理

一、Base關(guān)鍵詞的幾種用法

base重要用于OOP的多態(tài)上,base 關(guān)鍵字用于在派生類(lèi)中實(shí)現對基類(lèi)公有或者受保護成員的訪(fǎng)問(wèn),但是只局限在構造函數、實(shí)例方法和實(shí)例屬性訪(fǎng)問(wèn)器中

1、base調用基類(lèi)構造函數


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"); } }}

執行結果:

2、base在派生類(lèi)中調用基類(lèi)的方法


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(); } }}

執行結果:

二、this關(guān)鍵字

1、 this代表當前類(lèi)的實(shí)例對象


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}"); } }}

執行結果:

2、 用this串聯(lián)構造函數


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("有參構造函數"); } }}

執行結果:

3、擴展方法


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}"; } }}

執行結果:

4、索引器:參數可以是int,也可以是string等其他類(lèi)型


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關(guān)鍵字

1、new 運算符:用于創(chuàng )建對象和調用構造函數

(1)特點(diǎn)

  • 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異常。

(2)demo

static void Main(string[] args){ A a = new A();//new操作符 創(chuàng )建對象和調用構造函數 Console.ReadKey();}

2、new 修飾符:new 關(guān)鍵字可以顯式隱藏從基類(lèi)繼承的成員

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";}

3、new 約束:用于在泛型聲明中約束可能用作類(lèi)型參數的參數的類(lèi)型

new 約束指定泛型類(lèi)聲明中的任何類(lèi)型參數都必須有公共的無(wú)參數構造函數。如果要使用 new 約束,則該類(lèi)型不能為抽象類(lèi)型。當與其他約束一起使用時(shí),new() 約束必須最后指定:

public class A<T>where T : IComparable, new(){ ////}

四、override關(guān)鍵字

 要擴展或修改繼承的方法、屬性、索引器或事件的抽象實(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"); } }}

執行結果:

五、abstract關(guān)鍵字

  • 用關(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)。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C#基礎概念二十五問(wèn)
c#關(guān)鍵字
C#之虛函數
C#經(jīng)典筆試題
設計模式 2/23 工廠(chǎng)模式
.NET中的虛函數
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久