我是一个有用的助手,可以为您翻译文本。
我在使用第三方库时遇到了这种行为,在其中我需要隐藏和更改其中的一个方法。
我的设置如下:
为什么当我运行以下代码时会出现这种情况:
我在使用第三方库时遇到了这种行为,在其中我需要隐藏和更改其中的一个方法。
我的设置如下:
interface IBaseInterface
{
string MethodToHide();
}
class BaseClass : IBaseInterface
{
public string MethodToHide()
{
return "BaseClass";
}
}
class ChildClass : BaseClass
{
new public string MethodToHide()
{
return "ChildClass";
}
}
为什么当我运行以下代码时会出现这种情况:
var i = (IBaseInterface) (new ChildClass());
Console.WriteLine(i.MethodToHide());
输出结果为
BaseClass
,但是当将ChildClass的签名更改为
class ChildClass : BaseClass, IBaseInterface
输出结果为
ChildClass
为什么我需要显式地指定接口,才能让ChildClass隐藏BaseClass方法?