简体   繁体   English

为什么方法Queue#add()有布尔返回值?

[英]Why does method Queue#add() has boolean return value?

I revised knowledge about Queue . 我修改了关于Queue知识。 I watch to the Queue interface. 我看着Queue界面。

method add has following declaration : 方法add有以下声明:

boolean add(E e);

In java doc writes following: 在java doc中写道如下:

 * @return <tt>true</tt> (as specified by {@link Collection#add})

Hence this method can return only true!!! 因此这种方法只能返回真正的!!! Why does not this method return value declared as void ? 为什么这个方法没有返回声明为void值?

This method declaration is enough confusing for me. 这个方法声明对我来说足够混乱了。

If you look at the docs for Collection#add , it becomes clear: 如果你看一下Collection#add的文档,就会变得清晰:

true if this collection changed as a result of the call 如果此集合因调用而更改,则为true

Since the queue always changes as a result of the call, Queue#add always has to return true . 由于队列总是因调用而改变,因此Queue#add必须返回true (And it has to have a return value, in order to implement the interface correctly.) (并且它必须具有返回值,以便正确实现接口。)

Compare with Set , which also implements Collection , which will only add the element if it's not already in the set, and so might return false from add . Set ,它也实现了Collection ,它只会添加元素,如果它不在集合中,那么可能会从add返回false

Queue implements Collection which is a more generic interface. Queue实现了Collection ,这是一个更通用的接口。 In java, you can't implement or extend an interface without including all its methods as they are initially declared and thus, sometimes, you get classes with stub methods as add in this case. 在java中,您不能实现或扩展接口而不包括它们最初声明的所有方法,因此,有时候,在这种情况下,您将获得带有存根方法的类作为add

Collection add method is declared as: 集合添加方法声明为:

boolean add(E e)

So it has to be declared at Queue . 所以它必须在Queue声明。 In a list its return value will always be true given the nature of the queue data structure but not for others. 在列表中,根据队列数据结构的性质,其返回值始终为true,但对于其他数据结构则不然。

Imagine you are developing a new data structure implementing Collection interface which internally uses an array to store its elements. 想象一下,您正在开发一个实现Collection接口的新数据结构,该接口在内部使用数组来存储其元素。 In that case you may find useful to return false when adding new elements: You could already used all the array positions. 在这种情况下,您可能会发现在添加新元素时返回false非常有用:您可能已经使用了所有数组位置。

The method java.util.Queue.add(E) overrides the method java.util.Collection.add(E). 方法java.util.Queue.add(E)重写方法java.util.Collection.add(E)。 So, it has to follow the rules of method overriding. 因此,它必须遵循方法覆盖的规则。

One of this rule is that the return type of the method java.util.Queue.add(E) has to be a sub-type of the method java.util.Collection.add(E). 其中一条规则是java.util.Queue.add(E)方法的返回类型必须是方法java.util.Collection.add(E)的子类型。 Because the method java.util.Collection.add(E) returns a boolean, the method java.util.Queue.add(E) must also return a boolean. 因为方法java.util.Collection.add(E)返回一个布尔值,所以方法java.util.Queue.add(E)也必须返回一个布尔值。

The method java.util.Queue.add(E) is boolean true, because the queue is changed everytime we add new object into it. 方法java.util.Queue.add(E)是布尔值true,因为每次我们向其添加新对象时都会更改队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM