重寫、隱藏基類(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#生成excel示例sql數(shù)據(jù)庫導(dǎo)出excel
這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導(dǎo)出數(shù)據(jù)到excel,大家參考使用吧2014-01-01C#實(shí)現(xiàn)自定義FTP操作封裝類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)自定義FTP操作封裝類,涉及C#操作FTP的連接、傳輸、下載等操作的實(shí)現(xiàn)技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-03-03將Qt項目升級到Qt6吐血經(jīng)驗(yàn)總結(jié)
很多朋友向小編反饋將Qt項目升級到Qt6頻繁出錯,該如何處理呢,今天小編給大家?guī)砹藢t項目升級到Qt6吐血經(jīng)驗(yàn)總結(jié),感興趣的朋友一起看看吧2021-07-07C#中的Linq Intersect與Except方法使用實(shí)例
這篇文章主要介紹了C#中的Linq Intersect與Except方法使用實(shí)例,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01