重写基类方法与隐藏基类方法的区别
重写基类方法与隐藏基类方法的区别
其实要分析二者的区别,就是分析override和new这两个关键字在控制类的版本上的区别。
首先,我先列出本地MSDN 2008上与此有关的一些官方资料:
- “override(C# 参考)”:(ms-help://MS.MSDNQTR.v90.chs/dv_csref/html/dd1907a8-acf8-46d3-80b9-c2ca4febada8.htm)
- “new 修饰符(C# 参考)”:(ms-help://MS.MSDNQTR.v90.chs/dv_csref/html/a2e20856-33b9-4620-b535-a60dbce8349b.htm)
- “使用 Override 和 New 关键字进行版本控制(C# 编程指南)”:(ms-help://MS.MSDNQTR.v90.chs/dv_csref/html/88247d07-bd0d-49e9-a619-45ccbbfdf0c5.htm)
- “了解何时使用 Override 和 New 关键字(C# 编程指南)”:(ms-help://MS.MSDNQTR.v90.chs/dv_csref/html/323db184-b136-46fc-8839-007886e7e8b0.htm)
其次,我将以上资料中的一个例子进行简单修改,以更好地体现出二者在使用时的区别。
using System;
namespace KeywordsOfOverrideAndNew
{
// Define the base class
class Car
{
public virtual void DescribeCar()
{
System.Console.WriteLine("Call the DescribeCar() defined in Car class.");
}
}
// Define the derived classes
class ConvertibleCar : Car
{
// Use new
public new virtual void DescribeCar()
{
System.Console.WriteLine("Call the DescribeCar() defined in ConvertibleCar class.");
}
}
class Minivan : Car
{
// Use override
public override void DescribeCar()
{
System.Console.WriteLine("Call the DescribeCar() defined in Minivan class.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Declare object type by each classes :\n");
Car car1 = new Car();
car1.DescribeCar();
System.Console.WriteLine("----------");
ConvertibleCar car2 = new ConvertibleCar();
car2.DescribeCar();
System.Console.WriteLine("----------");
Minivan car3 = new Minivan();
car3.DescribeCar();
System.Console.WriteLine("----------");
Console.WriteLine("\n\n\n");
Console.WriteLine("Declare object type by the Car class :\n");
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("----------");
}
Console.Read();
}
}
}
/*
下面列出程序运行后的结果:
Declare object type by each classes :
Call the DescribeCar() defined in Car class.
----------
Call the DescribeCar() defined in ConvertibleCar class.
----------
Call the DescribeCar() defined in Minivan class.
----------
Declare object type by the Car class :
Call the DescribeCar() defined in Car class.
----------
Call the DescribeCar() defined in Car class.
----------
Call the DescribeCar() defined in Minivan class.
----------
*/
namespace KeywordsOfOverrideAndNew
{
// Define the base class
class Car
{
public virtual void DescribeCar()
{
System.Console.WriteLine("Call the DescribeCar() defined in Car class.");
}
}
// Define the derived classes
class ConvertibleCar : Car
{
// Use new
public new virtual void DescribeCar()
{
System.Console.WriteLine("Call the DescribeCar() defined in ConvertibleCar class.");
}
}
class Minivan : Car
{
// Use override
public override void DescribeCar()
{
System.Console.WriteLine("Call the DescribeCar() defined in Minivan class.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Declare object type by each classes :\n");
Car car1 = new Car();
car1.DescribeCar();
System.Console.WriteLine("----------");
ConvertibleCar car2 = new ConvertibleCar();
car2.DescribeCar();
System.Console.WriteLine("----------");
Minivan car3 = new Minivan();
car3.DescribeCar();
System.Console.WriteLine("----------");
Console.WriteLine("\n\n\n");
Console.WriteLine("Declare object type by the Car class :\n");
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("----------");
}
Console.Read();
}
}
}
/*
下面列出程序运行后的结果:
Declare object type by each classes :
Call the DescribeCar() defined in Car class.
----------
Call the DescribeCar() defined in ConvertibleCar class.
----------
Call the DescribeCar() defined in Minivan class.
----------
Declare object type by the Car class :
Call the DescribeCar() defined in Car class.
----------
Call the DescribeCar() defined in Car class.
----------
Call the DescribeCar() defined in Minivan class.
----------
*/
由生成的结果我们可以看出:
- 当使用基类和派生的类来声明对象时,各派生类对象都将执行各自的派生类方法。
- 当使用基类通过多态来声明对象时,使用 new 来隐藏基类方法的派生类由于使用了 new 关键字来定义此方法,由该派生类所创建的对象调用的不是派生类方法,而是基类方法。而使用 override 来重写基类方法的派生类所创建的对象调用的方法是派生类方法。
该文章经过本人整理所得,欢迎转载,转载时请加上本文地址;本文基于署名 2.5 中国大陆许可协议发布,请勿演绎或用于商业目的,但是必须保留本文的署名张志涛(包含链接如您有任何疑问或者授权方面的协商,请给我留言。