假如在基类中有一个virtual void A()方法,在派生类中使用new void A()和override void A()有什么区别,谢谢
解决方案 »
- DataGridView显示SQL中DECIMAL类型的时候,后面0太多,怎么去掉0?
- c# 应用程序重启问题
- 调用C++ DLL的一个奇怪
- Float.ToString()精度损失怎么办?
- 大家都是如何掌握类库的,我的想法不一定对,说说想让大家指点。(1)从net框架类库中大概根据其功能介绍找到自己可能需要的命名空间,(2)将这些命名空间中的类浏览一遍,根据类的功能选择自己可能今后需要的类。这两步大概需要3天时间。然后一般记忆这些命名空间和类
- 一个sql数据统计的问题!
- 程序员一天的郁闷之十(关ACCECC的表操作的)
- vs2005自带水晶报表部署
- ExecuteReader();问题
- 如何找到 “TestExample.TestComponent” 是属于哪一个 DLL 里面的 Class
- 错在哪?
- 请教大家,在继承中,abstract ,static,virtual是怎么理解的?
using System;namespace Override
{
class Base
{
public virtual void A()
{
Console.WriteLine("Base class virtual method A()");
}
}
class Derived1 : Base
{
public new void A()
{
Console.WriteLine("Derived1 class new method A()");
}
}
class Derived2 : Base
{
public override void A()
{
Console.WriteLine("Derived2 class override method A()");
} }
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Base b = new Base();
Derived1 b1 = new Derived1();
Derived2 b2 = new Derived2();
b.A();
b1.A();
b2.A(); Console.WriteLine(); b = b1;
b.A(); b = b2;
b.A();
}
}
}输出结果如下:
Base class virtual method A()
Derived1 class new method A()
Derived2 class override method A()Base class virtual method A()
Derived2 class override method A()
Press any key to continue由此可见:new是完全覆盖基类的成员函数,在运行时与基类的虚函数没有任何关系,new只是声明了在派生类中的一个重名函数而已
而override是重写基类的函数,在运行时可绑定到派生类的override方法,执行派生类的方法