Java - Interface extending itself

偶尔善良 提交于 2019-12-09 15:08:39

问题


I've been using this site for about 6 months now, and its time to ask my first question, because I cant find the answer to this, atleast not an answer that I can understand!

In this bit of code, why is this interface extending itself?

public interface PositionedVertex<V extends PositionedVertex<V>> {

/**
 * @return Position for node data.
 */
public Point getPosition();
}

Wouldnt this code do the same?:

public interface PositionedVertex<V> {

/**
 * @return Position for node data.
 */
public Point getPosition();
}

Thanks in advance!


回答1:


The interface isn't extending itself. The <V extends PositionedVertex<V>> is a bound on the generic type that is associated with your interface. It just means that the generic type parameter for any class that implements this interface must itself be a PositionedVertex.




回答2:


In the first case, you have bounded your generic type parameters to be subtype of the interface itself, whereas, in the second case, you can have any type as generic type parameter. So, they are potentially different declarations.

For example, you can define a reference like:

PositionedVertex<String>

for the 2nd interface type, but not for the 1st one.




回答3:


It is not extending itself. It is specifying that its generic parameter V must represent a type that extends or implements itself.




回答4:


There is no telling why without showing some more code. Or is this all? What it is actually telling you is that there is some type V used somewhere in this interface (as a parameter to a function, or as a return type) that is of the same type as the interface itself.




回答5:


This is extension for generic. More info about generics: http://docs.oracle.com/javase/tutorial/extra/generics/intro.html



来源:https://stackoverflow.com/questions/14861417/java-interface-extending-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!