我能否将实现接口(interface)的抽象类留空,并暗示接口(interface)中的所有方法/属性在我的类中都是抽象的。看来我必须在抽象类中再次写出它们,但我真的想避免这种重复。
我的原因是我有几个具有不同访问器的接口(interface),一个是公共(public)的,一个是内部的,我想把它们放在一起,所以我有一个抽象类来实现它们,然后可以扩展。
public interface ISomePublicProperties {
int PropertyOne {get;}
}
internal interface ISomeInternalProperties {
int PropertyTwo {get;}
}
public abstract class SomeClass : ISomePublicProperties, ISomeInternalProperties {}
但是编译器提示 SomeClass 没有实现接口(interface)方法 ISomePublicProperties.PropertyOne 和 ISomeInternalProperties.PropertyTwo
无论如何,在 C# 中(我知道 Java 允许这样做)我可以将抽象类留空实现接口(interface)吗?
最佳答案
没有。在 C# 中,抽象类必须完全实现接口(interface)。请注意,可以使用抽象方法和抽象属性来实现它。关于 C# 的一件小事一直困扰着我。
关于由空抽象类实现的 C# 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5495043/