本文實(shí)例講述了c#繼承中的函數(shù)調(diào)用方法,分享給大家供大家參考。具體分析如下:
首先看下面的代碼:
namespace Test
{
public class OnceChild : Base
{
protected override int Operate(int x, int y)
{
return x - y;
}
}
}
namespace Test
{
public class TwiceChild : OnceChild
{
protected override int Operate(int x, int y)
{
return x * y;
}
}
}
namespace Test
{
public class ThirdChild : TwiceChild
{
}
}
namespace Test
{
public class ForthChild : ThirdChild
{
protected new int Operate(int x, int y)
{
return x / y;
}
}
}
namespace Test
{
class Program
{
static void Main(string[] args)
{
Base b = null;
b = new Base();
b.Print();
b = new OnceChild();
b.Print();
b = new TwiceChild();
b.Print();
b = new ThirdChild();
b.Print();
b = new ForthChild();
b.Print();
}
}
}
從結(jié)果中可以看出:使用override重寫之后,調(diào)用的函數(shù)是派生的最遠(yuǎn)的那個(gè)函數(shù),使用new重寫則是調(diào)用new之前的派生的最遠(yuǎn)的函數(shù),即把new看做沒有重寫似的。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注