由空抽象类实现的 C# 接口
我可以将实现接口的抽象类保留为空,并暗示接口中的所有方法/属性在我的类中都是抽象的吗?看来我必须在抽象类中再次写出它们,但我真的想避免这种重复。
我的原因是我有几个带有不同访问器的接口,一个是公共的,一个是内部的,我想将它们组合在一起,这样我就有一个抽象类来实现它们,然后可以扩展它们。
public interface ISomePublicProperties {
int PropertyOne {get;}
}
internal interface ISomeInternalProperties {
int PropertyTwo {get;}
}
public abstract class SomeClass : ISomePublicProperties, ISomeInternalProperties {}
但编译器抱怨 SomeClass 没有实现接口方法 ISomePublicProperties.PropertyOne 和 ISomeInternalProperties.PropertyTwo
在 C# 中(我知道 Java 允许这样做)是否可以将抽象类留空实现接口?
Can I leave an abstract class that implements interfaces empty and imply that all the methods/properties in the interface are abstract within my class. It appears that I have to write them out again in the abstract class but I really want to avoid this duplication.
My reason is I have a couple of interfaces with different accessors, one public and one internal, that I want to bring together so I have an abstract class that implements them both that can then be extended.
public interface ISomePublicProperties {
int PropertyOne {get;}
}
internal interface ISomeInternalProperties {
int PropertyTwo {get;}
}
public abstract class SomeClass : ISomePublicProperties, ISomeInternalProperties {}
But the compiler complains that SomeClass does not implement interface method ISomePublicProperties.PropertyOne and ISomeInternalProperties.PropertyTwo
Is there anyway in C# (I know Java allows this) that I can leave the abstract class empty implementing interfaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有。在 C# 中,抽象类必须完全实现接口。请注意,它可以使用抽象方法和抽象属性来实现它。 C# 的一件小事一直困扰着我。
Nope. In C# the abstract class must fully implement the interface. Note it can implement it with abstract methods and abstract properties. It's one little thing about C# that has always bugged me.
唯一的方法是创建没有实现的虚拟属性。然后,它们将在派生类中被重写。
The only way to do it is create
virtual
properties with no implementation. These are then overridden in your derived classes.