码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-08-23 20:09:04      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:函数调用   []   protect   ret   ons   lin   console   string   ase   

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

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

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

标签:函数调用   []   protect   ret   ons   lin   console   string   ase   

原文地址:http://www.cnblogs.com/cddaishi/p/7419640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!