我有这个基类:
abstract class Base
{
public int x
{
get { throw new NotImplementedException(); }
}
}
以及以下后代:
class Derived : Base
{
public int x
{
get { //Actual Implementaion }
}
}
当我编译时,我收到此警告,指出派生类的定义x
将隐藏 Base 的版本。是否可以像方法一样重写 C# 中的属性?
你需要使用virtual
keyword
abstract class Base
{
// use virtual keyword
public virtual int x
{
get { throw new NotImplementedException(); }
}
}
或定义一个抽象属性:
abstract class Base
{
// use abstract keyword
public abstract int x { get; }
}
and use override
孩子时的关键词:
abstract class Derived : Base
{
// use override keyword
public override int x { get { ... } }
}
如果您不打算覆盖,您可以使用new
方法上的关键字隐藏父级的定义。
abstract class Derived : Base
{
// use new keyword
public new int x { get { ... } }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)