override重写基类方法

来源:互联网 发布:大数据产业统计口径 编辑:程序博客网 时间:2024/04/30 13:32

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

namespace Vehicles
{
    class Vehicle//车辆类
    {
        public void StartEngine(String noiseToMakeWhenStarting)
        {
            Console.WriteLine("Starting engine:{0}", noiseToMakeWhenStarting);
        }
        public void StopEngine(string noiseToMakeWhenStopping)
        {
            Console.WriteLine("Stopping engine:{0}", noiseToMakeWhenStopping);
        }
        public virtual void Drive()
        {
            Console.WriteLine("Default implementation of the drive method");
        }
    }
}

————————————————————————————————————

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

namespace Vehicles
{
    class Car:Vehicle//汽车类继承车辆类
    {
        public void Accelerate()//加速
        {
            Console.WriteLine("Accelerating");
        }
        public void Brake()//刹车
        {
            Console.WriteLine("Braking");
        }
        public override void Drive()
        {
            Console.WriteLine("motoring");
        }

    }
}

————————————————————————

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

namespace Vehicles
{
    class Airplane:Vehicle//飞机继承车辆类
    {
        public void TakeOff()//关闭飞机
        {
            Console.WriteLine("Taking off");

        }
        public void Land()//着陆
        {
            Console.WriteLine("Landing");
        }
        public override void Drive()
        {
            Console.WriteLine("Flying");
        }
    }
}

————————————————————————————

using System;

namespace Vehicles
{
    class Program
    {
        static void DoWork()
        {
           
            Console.WriteLine("journey by airplane:");
            Airplane myplane = new Airplane();
            myplane.StartEngine("Contact");
            myplane.TakeOff();
            myplane.Drive();
            myplane.Land();
            myplane.StartEngine("Whirr");
            Console.WriteLine("\nJourney by car:");
            Car myCar = new Car();
            myCar.StartEngine("Brm brm");
            myCar.Accelerate();
            myCar.Drive();
            myCar.Brake();
            myCar.StartEngine("phut phut");
            Console.WriteLine("\nTesting polymorphism");
            Vehicle v = myCar;//车辆类对象v= 我的车辆
            v.Drive();//car类的drive(重写了基类的drive)
            v = myplane;//车辆类对象v=我的飞机
            v.Drive();//airplane的drive(重写了基类的drive)
        }

        static void Main()
        {
            try
            {
                DoWork();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }
            Console.ReadKey();
        }
    }
}

——————————————————————

这是程序的源代码,汽车和飞机类继承交通交通工具类。交通工具类(基类)里面有Drive()方法,汽车和飞机类里面也有通过override重写的Drive()方法。所以在以后的调用Drive()函数都是调用的派生类的Drive();


 


 

原创粉丝点击