我一直认为base.Something
相当于((Parent)this).Something
,但显然事实并非如此.我认为重写方法消除了调用原始虚方法的可能性.
为什么第三个输出不同?
void Main() {
Child child = new Child();
child.Method(); //output "Child here!"
((Parent)child).Method(); //output "Child here!"
child.BaseMethod(); //output "Parent here!"
}
class Parent {
public virtual void Method() {
Console.WriteLine("Parent here!");
}
}
class Child : Parent {
public override void Method() {
Console.WriteLine ("Child here!");
}
public void BaseMethod() {
base.Method();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
375 次 |
最近记录: |