c#继承中的函数调用实例

2019-12-26 11:53:20王旭
易采站长站为您分析c#继承中的函数调用,实例分析了C#继承中的函数调用规律,有助于深入理解C#的继承,需要的朋友可以参考下    

本文实例讲述了c#继承中的函数调用方法,。具体分析如下:

首先看下面的代码:

 

复制代码 using System;
 
namespace Test
{
    public class Base
    {
        public void Print()
        {
            Console.WriteLine(Operate(8, 4));
        }
 
        protected virtual int Operate(int x, int y)
        {
            return x + y;
        }
    }
}

 

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;
        }
    }
}