[继承]3.调用基类的函数

Posted on 2009-10-08 14:20  Relax Active  阅读(294)  评论(0)    收藏  举报

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

namespace Demo10
{
    public class FatherClass       //声明一个基类!
    {
        public virtual decimal Caculate()      //声明一个有返回值的方法!
        {
            return 10M;                      
        }
        public class SonClass : FatherClass      //子类继承基类!
        {
            public override decimal Caculate()    //子类继承基类的方法!
            {
                return base.Caculate() * 0.8M;    //用关键字base调用基类的方法,并返回基类值的80%!
            }
        }

        static void Main(string[] args)
        {
            SonClass son = new SonClass();         //创建一个实例son!
            Console.WriteLine(son.Caculate());    //输出"8.0"!
        }
    }
}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3