C# 为什么我不能拥有受保护的接口成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/516148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I have protected interface members?
提问by ajlane
What is the argument against declaring protected-access members on interfaces? This, for example, is invalid:
反对在接口上声明受保护访问成员的论据是什么?例如,这是无效的:
public interface IOrange
{
public OrangePeel Peel { get; }
protected OrangePips Seeds { get; }
}
In this example, the interface IOrange
would guarantee that implementors at leastprovide an OrangePips
instance to their inheritors. If the implementor wanted to, they could expand the scope to full public
:
在这个例子中,接口IOrange
将保证实现者至少OrangePips
向他们的继承者提供一个实例。如果实现者愿意,他们可以将范围扩展到 full public
:
public class NavelOrange : IOrange
{
public OrangePeel Peel { get { return new OrangePeel(); } }
protected OrangePips Seeds { get { return null; } }
}
public class ValenciaOrange : IOrange
{
public OrangePeel Peel { get { return new OrangePeel(); } }
public OrangePips Seeds { get { return new OrangePips(6); } }
}
The intent of protected
members on interfaces is to provide a support contract for inheritors(sub-classes), for example:
protected
接口成员的意图是为继承者(子类)提供支持契约,例如:
public class SpecialNavelOrange : NavelOrange
{
...
// Having a seed value is useful to me.
OrangePips seeds = this.Seeds;
...
}
(Admittedly, this wouldn't work for struct
s)
(诚然,这对struct
s不起作用)
I can't see much of a case for private
or internal
modifiers in interfaces, but supporting both public
and protected
modifiers seems perfectly reasonable.
我在接口中看不到private
orinternal
修饰符的很多情况,但同时支持public
和protected
修饰符似乎是完全合理的。
I'm going to try explaining the utility of protected
members on interface
s by separating them from interface
s entirely:
我将尝试通过将protected
成员interface
与interface
s 完全分开来解释成员在s上的效用:
Let's imagine a new C# keyword, support
, to enforce inheritor contracts, so that we declare things as follows:
让我们想象一个新的 C# 关键字 ,support
来强制继承者契约,以便我们声明如下:
public support IOrangeSupport
{
OrangePips Seeds { get; }
}
This would allows us to contract classes to provide protected members to their inheritors:
这将允许我们收缩类以向其继承者提供受保护的成员:
public class NavelOrange : IOrange, IOrangeSupport
{
public OrangePeel Peel { get { return new OrangePeel(); } }
protected OrangePips Seeds { get { return null; } }
}
This is not particularly useful, because classes would already imply this contract by providing the protected
members in the first place.
这不是特别有用,因为类已经通过首先提供protected
成员来暗示这个契约。
But then we could also do this:
但是我们也可以这样做:
public interface IOrange : IOrangeSupport
{
...
}
Thereby applying IOrangeSupport
to all classes which implement IOrange
and requiring them to provide particular protected
members - which is not something we can currently do.
从而应用于IOrangeSupport
所有实现IOrange
并要求它们提供特定protected
成员的类 - 这不是我们目前可以做的。
采纳答案by Szymon Rozga
I think everyone hammered the point of an interface having only public members, no implementation details. What you are looking for is an abstract class.
我认为每个人都认为接口只有公共成员,没有实现细节。您正在寻找的是一个抽象类。
public interface IOrange
{
OrangePeel Peel { get; }
}
public abstract class OrangeBase : IOrange
{
protected OrangeBase() {}
protected abstract OrangePips Seeds { get; }
public abstract OrangePeel Peel { get; }
}
public class NavelOrange : OrangeBase
{
public override OrangePeel Peel { get { return new OrangePeel(); } }
protected override OrangePips Seeds { get { return null; } }
}
public class ValenciaOrange : OrangeBase
{
public override OrangePeel Peel { get { return new OrangePeel(); } }
protected override OrangePips Seeds { get { return new OrangePips(6); } }
}
Edit: It is fair to argue that if we have a PlasticOrange that derives from a class Ornament, it can only implement IOrange and not the Seeds protected method. That is fine. An interface by definition is a contract between a caller and an object, not between a class and its subclasses. The abstract class is as close as we come to this concept. And that is fine. What you are essentially proposing is another construct in the language through which we can switch subclasses from one base class to another without breaking the build. To me, this doesn't make sense.
编辑:公平地说,如果我们有一个派生自 Ornament 类的 PlasticOrange,它只能实现 IOrange 而不能实现 Seeds 保护方法。那没关系。根据定义,接口是调用者和对象之间的契约,而不是类与其子类之间的契约。抽象类与我们接近这个概念一样接近。这很好。您本质上提出的是语言中的另一种构造,通过它我们可以在不破坏构建的情况下将子类从一个基类切换到另一个基类。对我来说,这没有意义。
If you are creating a subclass of a class, the subclass is a specialization of the base class. It should be fully aware of any protected members of the base class. But if you suddenly want to switch the base class out, it makes no sense that the subclass should work with any other IOrange.
如果您要创建类的子类,则子类是基类的特化。它应该完全了解基类的任何受保护成员。但是如果你突然想切换基类,子类应该与任何其他 IOrange 一起工作是没有意义的。
I suppose you have a fair question, but it seems like a corner case and I don't see any benefit from it to be honest.
我想你有一个公平的问题,但它似乎是一个角落案例,老实说,我认为它没有任何好处。
回答by Gerrie Schenck
An interface is all about what a certain object can do, so when using a class which implements that interface the developer will expect all the members to be implemented, so the protected access modifier won't mean anything for interfaces.
接口是关于某个对象可以做什么的,因此当使用实现该接口的类时,开发人员将期望实现所有成员,因此受保护的访问修饰符对接口没有任何意义。
回答by Marc Gravell
Interface members area public API; things like protected
etc are implementation details - and interfaces don't haveany implementation. I suspect what you are looking for is explicit interface implementation:
接口成员是公共 API;之类的东西protected
等都是实现细节-和接口不具有任何实现。我怀疑您正在寻找的是显式接口实现:
public class NavelOrange : IOrange
{
public OrangePeel Peel { get { return new OrangePeel(); } }
OrangePips IOrange.Seeds { get { return null; } }
}
回答by Jason Punyon
An Interface contains only public members. Protected means whatever you're declaring is only available to the class and derived class instances.
接口仅包含公共成员。受保护意味着您声明的任何内容仅可用于类和派生类实例。
回答by JaredPar
Interfaces exist to allow people to access your class without knowing what the concrete implementation is. It completely divorices the implementation from the data passing contract.
接口的存在允许人们在不知道具体实现是什么的情况下访问您的类。它完全脱离了数据传递合约的实现。
Therefore, everything in an interface must be public. Non-public members are only useful if you have access to the implementation and therefore don't meaningfully contribute to an interface definition.
因此,接口中的所有内容都必须是公开的。非公共成员仅在您有权访问实现时才有用,因此不会对接口定义做出有意义的贡献。
回答by annakata
Because it makes no sense. An interface is a publicly exposed contract. I am an IThing, therefore I will perform IThing methods if asked. You can't ask an IThing to confirm it performs methods it can't tell you about.
因为没有意义。接口是公开暴露的合约。我是 IThing,因此如果被问到,我将执行 IThing 方法。你不能要求一个 IThing 来确认它执行它不能告诉你的方法。
回答by Anton Gogolev
Can't see why would one want this. If you want derived class to provide an implementation of a particular method, go for abstract base classes. Interfaces are just that - interfaces. A public contract, nothing else. Think of interface as of specification which describes how should the implementation look to the outside world. A specification for a two-pin plug does not state (at least I assume that) what it's internal structure should be like. It just must be interface-compatible with a plug socket.
(source: made-in-china.com)
不明白为什么会有人想要这个。如果您希望派生类提供特定方法的实现,请选择抽象基类。接口就是这样 - 接口。一个公共合同,没有别的。将接口视为规范,它描述了实现应该如何看待外部世界。两针插头的规范没有说明(至少我认为)它的内部结构应该是什么样的。它只是必须与插头插座接口兼容。
(来源:中国制造网)
回答by Frederick The Fool
There's sound judgment in current design of interfaces which is that it offers implementers greater flexibility. Remember that very often interfaces are written by framework programmers and implementers are different folks. To enforce implementation would be needlessly harsh.
当前的接口设计有一个合理的判断,即它为实现者提供了更大的灵活性。请记住,接口通常是由框架程序员编写的,而实现者则是不同的人。强制执行将是不必要的苛刻。
回答by Vojislav Stojkovic
An interface is a contract that promises certain functionality to clients. In other words, the purpose of an interface is to be able to cast a type into it and pass it around like that to code that needs the features guaranteed by that interface. Since client code of a type cannot access protected members of that type, it makes no sense to declare protected items in an interface.
接口是向客户端承诺某些功能的契约。换句话说,接口的目的是能够将类型转换到其中,并将其传递给需要该接口保证的功能的代码。由于某个类型的客户端代码无法访问该类型的受保护成员,因此在接口中声明受保护项是没有意义的。
回答by Brian Rasmussen
By implementing an interface the type states that it supports a specific set of methods. If any of these methods were not public, it would not be available to callers and thus the type would not support the interface as stated.
通过实现一个接口,该类型声明它支持一组特定的方法。如果这些方法中的任何一个不是公共的,则调用者将无法使用它,因此该类型将不支持所述接口。