重寫、隱藏基類(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("女");
}
}
輸出:
父
父
子
母
母
女
可見,在程序運行結(jié)果上new 和override是一樣的。
相關文章
C#實現(xiàn)一個相當全面的數(shù)據(jù)轉(zhuǎn)換工具類
這篇文章主要為大家介紹了如何使用C#編寫一個通用工具類DataConvert來進行數(shù)據(jù)轉(zhuǎn)換,包括30+個數(shù)據(jù)類型轉(zhuǎn)換,需要的可以了解一下2025-03-03
C# 延遲Task.Delay()和Thread.Sleep()的具體使用
Thread.Sleep()是同步延遲,Task.Delay()是異步延遲,本文主要介紹了C# 延遲Task.Delay()和Thread.Sleep()的具體使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2024-01-01

