集合框架中的接口
我的问题是
Interface Set 有方法 add(E e)
并且它扩展了接口 Collection
。 接口Collection
也有方法add(E e)
那么为什么我们需要在接口 Set 中使用相同的方法,因为它已经扩展了接口Collection
。 目的是什么? 我被困住了
My question is
Interface Set has method add(E e)
and it extends interface Collection
.
Interface Collection
also has method add(E e)
So why do we need the same method in interface Set , since it already extends interface Collection
.
What is the purpose ?
I am stuck with that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于这两个正确答案未能说服您,我将尝试解释一下。
接口定义契约 - 即它们定义实现者将要(和绑定)做什么,这样你就知道每当你通过接口引用一个对象时,它都有严格定义的行为,无论< em>它到底是如何实施的。
现在,这个契约分为两部分:
下面是具体的
Collection
/Set
示例:Collection
,那么您对以下内容一无所知add
的行为 - 是否允许重复,Set
,那么您可以确定不允许重复。这种区别是由 redefine
add
方法的 javadoc 实现的。Since the two correct answers didn't manage to convince you, I'll try to explain.
Interfaces define contracts - i.e. they define what implementors are going (and bound) to do, so that you know that whenever you refer to an object by an interface, it has a strictly defined behaviour, no matter how exactly it has been implemented.
Now, this contract comes in two parts:
And here comes the concrete
Collection
/Set
example:Collection
, then you don't know anything of the behaviour ofadd
- whether it allows duplicates or notSet
, then you are certain that no duplicates are allowed.This distinction is made by the javadoc of the redefined
add
method.设置.add
完善了Collection.add
。来自后者的 Javadoc:这就是
Set.add
的 Javadoc 中所做的事情,其中指出,例如,不会将重复元素添加到集合中。更新:关于契约和接口
(包括并扩展我下面的评论以完善这个答案。)
方法的契约指定 - 正式或非正式 - 调用者期望提供作为输入的内容对于该方法,以及该方法调用的保证结果是什么。例如,合约可能声明不需要
null
参数,并且如果该方法传递了null
参数,它将抛出NullPointerException
。 Collection 框架中方法的 Javadoc 是此类契约的好例子。请注意,某些语言允许甚至要求契约的正式定义,因此契约被编译到代码中并主动执行运行时。 Eiffel 就是这样一种语言。然而,Java 没有这样的功能; Javadoc中定义的契约并不正式,甚至没有为它们定义严格的格式。这些仅供人类阅读,JVM 不会注意到。因此,在 Java 中违反契约可能不会立即被注意到,只有稍后导致的错误开始出现时才会被注意到。
可以为具体类方法和抽象/接口方法定义契约。接口的契约(应该)绑定到它的所有实现。即
HashSet.add
、TreeSet.add
、LinkedHashSet.add
等都必须履行Set.add
的约定>(并可能进一步完善)。不按照Set.add
契约行事的实现会破坏 Liskov替换原则。Set.add
refines the contract ofCollection.add
. From the latter's Javadoc:That's what is done in the Javadoc of
Set.add
, where it states that e.g. duplicate elements are not added to the set.Update: on contracts and interfaces
(including and extending my comments below to round out this answer.)
The contract of a method specifies - formally or informally - what the caller is expected to provide as input for that method, and what is the guaranteed outcome of the method call. E.g. the contract may state that no
null
parameters are expected, and if the method is passed anull
parameter, it will throw aNullPointerException
. The Javadoc of methods in the Collection framework are good examples of such contracts.Note that some languages allow or even require the formal definition of contracts, thus the contracts are compiled into the code and actively enforced runtime. Eiffel is such a language. However, Java has no such facility; the contracts defined in Javadoc are not formal, there is not even a strict format defined for them. These are meant only for human reading, left unnoticed by the JVM. Thus, breaking a contract in Java may not be immediately noticeable, only later when the resulting bugs start to appear.
Contracts can be defined both for concrete class methods and abstract/interface methods. The contract of an interface is (should be) binding to all of its implementations. I.e.
HashSet.add
,TreeSet.add
,LinkedHashSet.add
etc. all must fulfill the contract ofSet.add
(and may further refine it). An implementation which does not behave according to the contract ofSet.add
breaks the Liskov substitution principle.据我所知,扩展类应该具有与超类指定的相同方法。至于 add 方法之间的差异,我建议您比较各个
add()
的 javadocAs far as I know extended classes should have the same methods as specified by the superclass. As for the differences between the add methods I suggest you compare the javadocs for the respective
add()
s