重寫、隱藏基類(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是一樣的。
相關(guān)文章
c#生成excel示例sql數(shù)據(jù)庫導(dǎo)出excel
這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導(dǎo)出數(shù)據(jù)到excel,大家參考使用吧2014-01-01C#中的Linq Intersect與Except方法使用實例
這篇文章主要介紹了C#中的Linq Intersect與Except方法使用實例,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06