重寫、隱藏基類(new, override)的方法
public class Father
{
public void Write() {
Console.WriteLine("父");
}
}
public class Mother
{
public virtual void Write()
{
Console.WriteLine("母");
}
}
public class Boy : Father
{
public new void Write()
{
Console.WriteLine("子");
}
}
public class Girl : Mother
{
public override void Write()
{
Console.WriteLine("女");
}
}
static void Main(string[] args)
{
Father father = new Boy();
father.Write();
Boy boy = new Boy();
boy.Write();
Mother mother = new Mother();
mother.Write();
Girl girl = new Girl();
girl.Write();
Console.ReadLine();
}
輸出:
父
子
母
女
添加調(diào)用父方法:
public class Boy : Father
{
public new void Write()
{
base.Write();
Console.WriteLine("子");
}
}
public class Girl : Mother
{
public override void Write()
{
base.Write();
Console.WriteLine("女");
}
}
輸出:
父
父
子
母
母
女
可見,在程序運(yùn)行結(jié)果上new 和override是一樣的。
相關(guān)文章
C#實(shí)現(xiàn)一個(gè)相當(dāng)全面的數(shù)據(jù)轉(zhuǎn)換工具類
這篇文章主要為大家介紹了如何使用C#編寫一個(gè)通用工具類DataConvert來(lái)進(jìn)行數(shù)據(jù)轉(zhuǎn)換,包括30+個(gè)數(shù)據(jù)類型轉(zhuǎn)換,需要的可以了解一下2025-03-03
C#?wpf利用附加屬性實(shí)現(xiàn)任意控件拖動(dòng)
這篇文章主要為大家詳細(xì)介紹了C#?WPF如何利用附加屬性對(duì)幾種拖動(dòng)方式進(jìn)行封裝,實(shí)現(xiàn)復(fù)用性,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
C# 延遲Task.Delay()和Thread.Sleep()的具體使用
Thread.Sleep()是同步延遲,Task.Delay()是異步延遲,本文主要介紹了C# 延遲Task.Delay()和Thread.Sleep()的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01

