要访问父成员,您必须强制转换对象。
public class A
{
public virtual void One();
public void Two();
}
public class B : A
{
public override void One();
public new void Two();
}
B b = new B();
A a = b as A;
a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B
来源:方法签名中的新关键字
建议:你隐藏了一个继承的成员。您应该像这样使用“new”关键字:
class Parent
{
public void MethodA()
{
Console.WriteLine("Parent");
}
}
class Child : Parent
{
public new void MethodA()
{
Console.WriteLine("Child");
}
}