正如此MSDN 文章中所引用的:
派生类的类型对象无法访问从基类继承的重新定义的新方法,继承的ShowDetails()
Method内部的Method对派生类对象的调用是对基类DescribeCar()
的ShowDetails()
Method进行的。
如果DescribeCar()
Method也是ConvertibleCar
类可用的,怎么就看不到所谓的new ShowDetails()
Method呢?
class Car
{
public void DescribeCar()
{
System.Console.WriteLine("Four wheels and an engine.");
ShowDetails();
}
public virtual void ShowDetails()
{
System.Console.WriteLine("Standard transportation.");
}
}
// Define the derived classes.
// Class ConvertibleCar uses the new modifier to acknowledge that ShowDetails
// hides the base class method.
class ConvertibleCar : Car
{
public new void ShowDetails()
{
System.Console.WriteLine("A roof that opens up.");
}
}
class Program
{
static void Main(string[] args)
{
ConvertibleCar car2 = new ConvertibleCar();
car2.DescribeCar();
}
}
//output
// Four wheels and an engine.
// Standard transportation.