如果我有这样的事情:
class Base
{
public void Write()
{
if (this is Derived)
{
this.Name();//calls Name Method of Base class i.e. prints Base
((Derived)this).Name();//calls Derived Method i.e prints Derived
}
else
{
this.Name();
}
}
public void Name()
{
return "Base";
}
}
class Derived : Base
{
public new void Name()
{
return "Derived";
}
}
并使用以下代码调用它,
Derived v= new Derived();
v.Write(); // prints Base
然后Name调用基类的方法。this但是该方法中关键字的实际类型是Write什么?如果那是Derived类型(当程序控件进入Write方法中的第一个 if 块时),那么它正在调用基Name方法,为什么显式转换(Derived)this,改变对Name派生类方法的调用?