[英]Can I override a property in c#? How?
I have this Base class:我有这个基类:
abstract class Base
{
public int x
{
get { throw new NotImplementedException(); }
}
}
And the following descendant:以及以下后代:
class Derived : Base
{
public int x
{
get { //Actual Implementaion }
}
}
When I compile I get this warning saying Derived class's definition of x
is gonna hide Base's version of it.当我编译时,我收到这个警告说派生类的x
定义将隐藏它的 Base 版本。 Is is possible to override properties in c# like methods?是否可以像方法一样覆盖 c# 中的属性?
You need to use virtual
keyword您需要使用virtual
关键字
abstract class Base
{
// use virtual keyword
public virtual int x
{
get { throw new NotImplementedException(); }
}
}
or define an abstract property:或定义一个抽象属性:
abstract class Base
{
// use abstract keyword
public abstract int x { get; }
}
and use override
keyword when in the child:并在孩子中使用override
关键字:
abstract class Derived : Base
{
// use override keyword
public override int x { get { ... } }
}
If you're NOT going to override, you can use new
keyword on the method to hide the parent's definition.如果您不打算覆盖,则可以在方法上使用new
关键字来隐藏父级的定义。
abstract class Derived : Base
{
// use new keyword
public new int x { get { ... } }
}
Make the base property abstract and override or use the new keyword in the derived class.使基本属性抽象并覆盖或在派生类中使用 new 关键字。
abstract class Base
{
public abstract int x { get; }
}
class Derived : Base
{
public override int x
{
get { //Actual Implementaion }
}
}
Or或者
abstract class Base
{
public int x { get; }
}
class Derived : Base
{
public new int x
{
get { //Actual Implementaion }
}
}
Change property signature as shown below:更改属性签名如下所示:
Base class基类
public virtual int x
{ get { /* throw here*/ } }
Derived class派生类
public override int x
{ get { /*overriden logic*/ } }
If you do not need any implementation in Base class just use abstract property.如果您不需要基类中的任何实现,只需使用抽象属性。
Base:根据:
public abstract int x { get; }
Derived:衍生的:
public override int x { ... }
I would suggest you using abstract
property rather than trhowing NotImplemented exception in getter, abstact
modifier will force all derived classes to implement this property so you'll end up with compile-time safe solution.我建议您使用abstract
属性而不是在 getter 中抛出 NotImplemented 异常, abstact
修饰符将强制所有派生类实现此属性,因此您最终会得到编译时安全的解决方案。
abstract class Base
{
public virtual int x
{
get { throw new NotImplementedException(); }
}
}
or或者
abstract class Base
{
// use abstract keyword
public abstract int x
{
get;
}
}
In both case you have to write in the derived class在这两种情况下,您都必须在派生类中编写
public override int x
{
get { your code here... }
}
difference between the two is that with abstract you force the derived class to implement something, and with virtaul you can provide a default behavior that the deriver can use as is, or change.两者之间的区别在于,使用 abstract 可以强制派生类实现某些东西,而使用 virtaul 可以提供派生者可以按原样使用或更改的默认行为。
abstract class Base
{
// use abstract keyword
public virtual int x
{
get { throw new NotImplementedException(); }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.