virtual与override

virtual与override

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{   
    public class MyBaseClass
    {
        public virtual string DoSomething() // 可以重写
        {
            return "Hello world";
        }
    }

    public class MyDerivedClass:MyBaseClass
    {
        public override string DoSomething() // 重写基类函数
        {
            //return base.DoSomething
            return "Hello china";
        }

        static void Main(string[] args)
        {
            MyDerivedClass objectA = new MyDerivedClass();


            Console.WriteLine("DoSomething = {0}", objectA.DoSomething());

            Console.ReadKey();
        }
    }
}

子类重写了基类的方法。


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6763301.html,如需转载请自行联系原作者