c#隱藏基類方法的作用
當派生類和基類有同樣的的方法(方法名相同、參數(shù)列表相同和返回值相同),這時派生類的方法可以隱藏基類的方法。也就是說可以在派生類中創(chuàng)建和基類方法相同的方法,但是執(zhí)行的過程卻不同,并且需要使用new關(guān)鍵字。
class program
{
static void Main(string[] args)
{
B b=new B();
b.F();
A a=b;
a.F();
Console.ReadKey();
}
}
class A
{
public void F()
{
Console.WriteLine("A.F");
}
}
class B:A
{
new public void F() //隱藏A類中的F方法
{
Console.WriteLine("B.F");
}
}
相關(guān)文章
c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的區(qū)別詳解
這篇文章主要介紹了c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的區(qū)別,需要的朋友可以參考下2014-07-07Winform使用DataGridView實現(xiàn)下拉篩選
這篇文章主要為大家詳細介紹了Winform如何使用原生DataGridView實現(xiàn)下拉篩選功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-09-09