重写基类方法与隐藏基类方法的区别
2013-05-15 17:16
211 查看
重写基类方法与隐藏基类方法的区别
其实要分析二者的区别,就是分析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.
----------
*/
由生成的结果我们可以看出:
当使用基类和派生的类来声明对象时,各派生类对象都将执行各自的派生类方法。
当使用基类通过多态来声明对象时,使用 new 来隐藏基类方法的派生类由于使用了 new 关键字来定义此方法,由该派生类所创建的对象调用的不是派生类方法,而是基类方法。而使用 override 来重写基类方法的派生类所创建的对象调用的方法是派生类方法。
相关文章推荐
- 关于C#中,继承类重写与隐藏基类方法的区别!
- c#隐藏和重写基类方法的区别
- C#继承类重写与隐藏基类方法的区别
- C#继承类重写与隐藏基类方法的区别
- c#隐藏和重写基类方法的区别
- c#隐藏基类方法和重写基类方法的区别
- c#隐藏基类方法和重写基类方法的区别
- C#继承类重写与隐藏基类方法的区别
- [导入]C#继承类重写与隐藏基类方法的区别
- 重写、隐藏基类(new, override)的方法
- 隐藏(new)和方法重写(override)和重载(overload)的区别
- C#中隐藏(new)和方法重写(override)和重载(overload)的区别
- 重写、隐藏基类方法(new, override)
- C# 基类 派生类 方法隐藏 方法重写
- 虚方法的重写与普通方法的隐藏的区别;抽象方法和虚方法有什么区别?
- 隐藏基类方法和改写基类方法的区别
- C#中隐藏(new)和方法重写(override)和重载(overload)的区别
- 2018/01/07JAVA 基础 / 接口与继承:重写【子类继承并覆盖父类的对象方法】、隐藏【子类继承并覆盖父类的类方法】与实现类实现接口方法の区别
- C#和Java 方法重写和覆盖隐藏的区别
- 【经典】C#中隐藏(new)、方法重写(override)、重载(overload)的区别