[英]Java Documentation: What is meaning of 'methods inherited from interface X
I must be missing some basic Java terminology here: 我一定在这里缺少一些基本的Java术语:
Classes can be extended, therefore their methods can be inherited by their sub-classes. 可以扩展类,因此它们的方法可以被其子类继承 。
Interfaces can be implemented . 接口可以实现 。 An implementing class will have to implement all of the interface's methods - the interface itself does not implement anything, only declares.
一个实现类将必须实现该接口的所有方法-接口本身不实现任何东西,仅声明。
So, how come when I look at the documentation of HashSet ( https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html ), I see a list of methods which are inherited from interface java.util.Set ? 因此,当我查看HashSet的文档( https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html )时,为什么会看到从中继承的方法列表接口 java.util.Set?
I guess you are referring to the statements like this from the generated JavaDoc HTML: 我猜您是从生成的JavaDoc HTML中引用这样的语句:
Methods inherited from interface java.util.Set ...
从接口java.util.Set继承的方法...
Inheritance in this sense means that the signature of the methods in question is inherited, but not necessarily the implementation. 从这个意义上讲,继承意味着所讨论方法的签名是被继承的,但不一定是实现的。 The reason for that is simple: In Java you usually do not look into the implementations of third party code, but only into the interfaces with their signatures and JavaDoc.
原因很简单:在Java中,您通常不关注第三方代码的实现,而仅关注具有其签名和JavaDoc的接口。
So, basically, the signatures of those methods are inherited from interface Set
and implemented in the HashSet
or AbstractSet
. 因此,基本上,这些方法的签名都是从
Set
接口继承的,并在HashSet
或AbstractSet
。 Hence, actually it is implementing the interface Set
. 因此,实际上它正在实现接口
Set
。
Sidenote : In Java 8, you can have Interfaces implementing methods, but that's a different story. 旁注 :在Java 8中,您可以使用接口实现方法,但这是另一回事。
I think this has more to do with the javadoc than with the language. 我认为这与javadoc的关系多于与语言的关系。 In Java, all the methods in the interface have to be implemented.
在Java中,必须实现接口中的所有方法。 So from a language standpoint, there's no real difference betwen
add
and addAll
. 因此,从语言的角度来看,
add
和addAll
没有真正的区别。 Both are declared in Set
; 两者都在
Set
中声明; HashSet
is a concrete class; HashSet
是一个具体的类; therefore it must provide an implementation for both. 因此,它必须为两者提供实现。
The difference really just has to do with whether the author had anything to add to the javadoc in the interface. 真正的区别仅在于作者是否在界面中向javadoc添加了任何内容。 For
add
, it's necessary to add javadoc to HashSet
, because Set
defines add
as an optional operation (it could be implemented by throwing an exception), therefore HashSet
needs to specify that add
actually does something useful. 对于
add
,有必要将javadoc添加到HashSet
,因为Set
将add
定义为可选操作(可以通过抛出异常来实现),因此HashSet
需要指定add
实际上执行了一些有用的操作。 For addAll
, however, there's no need to add any documentation in HashSet
that wasn't already in Set
's javadoc. 对于
addAll
,不需要在Set
的javadoc中尚未在HashSet
中添加任何文档。
So I think that the javadoc page is slightly inaccurate; 因此,我认为javadoc页面略有错误; it really should say that the "javadoc" is inherited from
Set
, not the methods. 确实应该说“ javadoc”是继承自
Set
而不是方法。 (Technically, the methods aren't inherited, because an abstract method from an interface isn't inherited if there's another method with the same signature--see JLS 8.4.8 . That applies equally to all methods declared in the interface, whether or not the javadoc says they're "inherited".) However, saying "Documentation inherited from class java.util.Set" might look a little odd to readers. (从技术上讲,方法不会被继承,因为如果存在另一个具有相同签名的方法,则不会继承接口的抽象方法,请参见JLS 8.4.8 。这同样适用于接口中声明的所有方法,无论是但是,说“从类java.util.Set继承的文档”对读者来说可能有点奇怪。 So I'm OK with a slight technical inaccuracy here if it conveys the message adequately.
因此,如果可以充分传达信息,我在这里会有一点技术上的不正确之处。 Most readers wouldn't notice the inaccuracy, and it really doesn't matter.
大多数读者不会注意到这种不准确性,这真的没有关系。 In fact, I didn't notice this little flaw in the javadoc until you posted this question--and I'm someone who used to work on a compiler for a different language and spent many hours reading the language standard and delving into the exact definitions of the terms so that I could find out exactly what the standard required.
实际上,直到您发布了这个问题,我才注意到javadoc中的这个小缺陷-我是一个曾经使用不同语言的编译器并花了许多时间阅读语言标准并深入研究其确切内容的人。术语的定义,以便我可以准确地找到所需的标准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.