我正在java中使用树,并且我有一个带有自引用的简单无序树的以下接口(interface):
public interface Node<N extends Node> {
public N getParent();
public void setParent(N parent);
public Collection<N> getChildren();
public void addChild(N node);
public void removeChild(N node);
public N getRootNode();
... more ...
}
当然的想法是创建一个类型安全的节点
public abstract class ParentChildNode<E extends Node> implements Node<E> {
真正让我烦恼的问题是我收到的警告:
ParentChild is a raw type. References to generic type Node<N> should be parameterized
warning on this line:
public interface Node<N extends Node> {
warning on this line:
public abstract class ParentChildNode<E extends ParentChild> implements ParentChild<E>
我能做到:
public interface Node<N extends Node<?>> {
但我担心我会陷入恶心领域。我可以抑制警告,但我工作的地方不允许这样做。
有什么建议吗?我注意到 java.util collections API 在任何地方都没有任何警告。
提前致谢
最佳答案
像这样的东西怎么样(与Enum
类的声明并行):
public interface Node<N extends Node<N>> {
public N getParent();
public void setParent(N parent);
public Collection<N> getChildren();
public void addChild(N node);
public void removeChild(N node);
public N getRootNode();
... more ...
}
public abstract class ParentChildNode implements Node<ParentChildNode> {
关于java - 如何避免java中自引用类型安全接口(interface)的参数类型警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3393708/