代码如下:
internal class Base
{
public virtual void Do()
{
Console.WriteLine( "This is base method" );
}
} internal class Derived : Base
{
public new virtual void Do()
{
Console.WriteLine( "This is the derived class's hidden method!" );
}
} Base b = new Derived();
b.Do();输出:This is base method
可是按照CLR运行机制:在调用虚实例方法时,首先检查发出调用的变量,然后跟随地址来到发出调用的对象,代码检查对象内部的“类型对象指针”,它指向对象的实际类型,然后,代码在类型对象的方法表中查找引用了被调用方法的记录项,接着调用方法。
按照这个理论上应该输出This is the derived class's hidden method!才对,或者因为使用了隐藏方法,派生类的方法表有什么特殊之处?
求解!
internal class Base
{
public virtual void Do()
{
Console.WriteLine( "This is base method" );
}
} internal class Derived : Base
{
public new virtual void Do()
{
Console.WriteLine( "This is the derived class's hidden method!" );
}
} Base b = new Derived();
b.Do();输出:This is base method
可是按照CLR运行机制:在调用虚实例方法时,首先检查发出调用的变量,然后跟随地址来到发出调用的对象,代码检查对象内部的“类型对象指针”,它指向对象的实际类型,然后,代码在类型对象的方法表中查找引用了被调用方法的记录项,接着调用方法。
按照这个理论上应该输出This is the derived class's hidden method!才对,或者因为使用了隐藏方法,派生类的方法表有什么特殊之处?
求解!
解决方案 »
- 时钟事件的注销
- 解除socket绑定
- 后台线程监控WEB页面如何实现
- 讨论一下c/s架构的多客户端数据同步问题
- c# 光标问题 在线等。。。
- 请大家帮忙看一下我的SQL-存储过程哪里出现了问题....
- 请教一个有关ObjectList的简单问题 散分20
- iis配置问题!!!大家帮帮我,急!!!
- BindingSource.Filter的表达式问题
- File f1 = new File("Class1.cs"); ( 重载“File”方法未获取“1”参数)
- 方法返回DataSet时报错Index (zero based) must be greater than or equal to zero and less.
- 用C#图书管理系统
2、不满足上述情况的调用,则调用基类的方法。其实这里的new(故意隐藏父类同名成员)只是故意制造混淆的。如果将Derived的Do方法,改写为重写父类的方法,则结果会显示
This is the derived class's hidden method!