[英]c# Properties in Abstract Base Classes
I have a strange problem that I could not solve. 我有一个我无法解决的奇怪问题。 When I try to compile the following snipped I get this error:
当我尝试编译以下剪切时,我收到此错误:
'AbstractClass' does not implement interface member 'Property' (Compiler Error CS0535) 'AbstractClass'没有实现接口成员'Property'(编译器错误CS0535)
The online help tells me to make my AbstractClass abstract, which it is. 在线帮助告诉我使我的AbstractClass抽象,它是。 Can anybody tell me where I went wrong?
谁能告诉我哪里出错了?
Cheers Rüdiger 干杯Rüdiger
public interface IBase {
string Property { get; }
}
public abstract class AbstractClass : IBase
{
public override string ToString()
{
return "I am abstract";
}
}
public class ConcreteClass : AbstractClass
{
string Property {
get {
return "I am Concrete";
}
}
}
Your AbstractClass
needs to provide an implementation for Property
from the IBase
interface, even if it's just abstract itself: 您的
AbstractClass
需要从IBase
接口提供Property
的实现,即使它只是抽象本身:
public abstract class AbstractClass : IBase
{
public override string ToString()
{
return "I am abstract";
}
public abstract string Property { get; }
}
Update: Luke is correct that the concrete implementation will need to specify that Property
is an override, otherwise you'll get a "does not implement inherited abstract member" error: 更新: Luke是正确的,具体实现需要指定
Property
是一个覆盖,否则你将得到一个“不实现继承的抽象成员”错误:
public class ConcreteClass : AbstractClass
{
public override string Property {
get {
return "I am Concrete";
}
}
}
You have to add abstract implementation of the property to your AbstractClass : 您必须将属性的抽象实现添加到AbstractClass:
public abstract class AbstractClass : IBase
{
public override string ToString()
{
return "I am abstract";
}
public abstract string Property { get; }
}
And the override keyword to the Concrete class 以及Concrete类的override关键字
AbstractClass应该实现包含Property的IBase,你还没有实现它
You abstract class does not implement the interface IBase
. 你抽象类没有实现
IBase
接口。 Just add the property Property
to AbstractClass
. 只需将属性
Property
添加到AbstractClass
。
public abstract String Property { get; }
You need to declare Property
in AbstractClass
so that it fulfills the IBase
contract. 您需要在
AbstractClass
声明Property
以使其满足IBase
契约。
If you want ConcreteClass
to be able to override Property
then you should declare it as abstract
or virtual
. 如果您希望
ConcreteClass
能够覆盖Property
那么您应该将其声明为abstract
或virtual
。 Then you'll need to declare the ConcreteClass
implementation of Property
with override
. 然后,您需要使用
override
声明Property
的ConcreteClass
实现。
public interface IBase
{
string Property { get; }
}
public abstract class AbstractClass : IBase
{
public abstract string Property { get; }
public override string ToString()
{
return "I am abstract";
}
}
public class ConcreteClass : AbstractClass
{
public override string Property
{
get
{
return "I am Concrete";
}
}
}
You need to implement the IBase -property Property like this: 您需要像这样实现IBase -property Property :
public abstract class AbstractClass : IBase
{
public override string Property()
{
return "This is the base-class implementation";
}
}
Or make it abstract . 或者让它抽象化 。
您必须在抽象类中实现Property属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.