msdn上的一个例子看不太懂,请高手讲解一下,关于virtual,new,override的
/*
如果你安装了 msdn for visual studio 2005, 下面这段代码可以在这里找到:
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_csref/html/323db184-b136-46fc-8839-007886e7e8b0.htm
*/
using System;
// Define the base class
class Car
{
public virtual void DescribeCar()
{
System.Console.WriteLine( "Four wheels and an engine. ");
}
}
// Define the derived classes
class ConvertibleCar : Car
{
public new virtual void DescribeCar()
{
base.DescribeCar();
System.Console.WriteLine( "A roof that opens up. ");
}
}
class Minivan : Car
{
public override void DescribeCar()
{
base.DescribeCar();
System.Console.WriteLine( "Carries seven people. ");
}
}
public class test
{
public static void Main()
{
Car[] cars = new Car[3];
cars[0] = new Car();
cars[1] = new ConvertibleCar();
cars[2] = new Minivan();
foreach (Car vehicle in cars)
{
System.Console.WriteLine( "Car object: " + vehicle.GetType());
vehicle.DescribeCar();
System.Console.WriteLine( "----------\n ");
}
Console.ReadKey();
}
}
输出的结果为什么不是:
Four wheels and an engine.
----------
Four wheels and an engine.
A roof that opens up.
----------
Four wheels and an engine.
Carries seven people.
----------
呢。
请高手们用最通俗的语言讲