class BaseClass{ public void Method1()
{
Console.WriteLine("Base - Method1");
Method2();
} public virtual void Method2()
{
Console.WriteLine("Base - Method2");
}
}class DerivedClass : BaseClass{ public new void Method2()
{
Console.WriteLine("Derived - Method2");
}
}DerivedClass dc = new DerivedClass();dc.Method1();結(jié)果為Base - Method1 Base - Method2請問為什么得到的結(jié)果不是Base - Method1 Derived - Method2?new不是會隱藏基類的成員嗎?
1 回答

蠱毒傳說
TA貢獻(xiàn)1895條經(jīng)驗 獲得超3個贊
只是隱藏,還是會調(diào)用的啦,又沒消失,你要想用子類的方法實(shí)現(xiàn)就去掉new,通過override關(guān)鍵字進(jìn)行重寫即可。
- 1 回答
- 0 關(guān)注
- 649 瀏覽
添加回答
舉報
0/150
提交
取消