java 通用接口以 self 作为参数。递归泛型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1330901/
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
Generic Interface takes self as Parameter. Recursive Generic?
提问by Daniel Bingham
Disclaimer: I don't have a whole ton of experience with Java Generics, but my colleagues and I just spent a solid hour trying to decipher an interface that was structured like this:
免责声明:我对 Java 泛型没有丰富的经验,但我和我的同事只是花了一个小时试图破译一个结构如下的接口:
interface HasAttributes<A extends HasAttributes<A, B>,
B extends HasAttributesType<B>> extends Identification<B> {
What exactly does it mean when an interface generic takes a type parameter that is itself? What does this do?
当接口泛型采用本身的类型参数时,这究竟意味着什么?这有什么作用?
采纳答案by Joshua McKinnon
There is meaning to this - Java's Enum class is a good example of a simliar situation:
这是有意义的 - Java 的 Enum 类是类似情况的一个很好的例子:
public abstract class Enum<E extends Enum<E>>
implements Comparable<E>, Serializable
There are some enlightening answers in this Stack Overflow question about Enumthat should shed some light on this particular use of generics for you, as well as answer this more elegantly than I could.
在这个关于 Enum 的 Stack Overflow 问题中有一些有启发性的答案,它们应该为您阐明这种泛型的特殊用法,并且比我能更优雅地回答这个问题。

