这可以按预期工作 - (非静态)内部类Node使用Tree的参数(K,V)进行参数化:
public class Tree<K,V> {
private abstract class Node extends Page<K,V> {}
private final class InnerNode extends Node {}
private final class LeafNode extends Node {}
}
现在我想以某种方式向编译器传达内部节点应该只接受整数作为参数。尝试:
private abstract class Node<V> extends Page<K, V> {}
但它显示警告:
类型参数V隐藏类型V
我想实现:
public class Tree<K, V> {
/** The type parameter V is hiding the type V */
private abstract class Node<V> extends Page<K, V> {}
private final class InnerNode extends Node<Integer> {}
private final class LeafNode<V> extends Node<V> {}
}
但是V是Tree的类型参数。下面不会编译:
public class Tree<K, V> {
private abstract class Node<T extends V> extends Page<K, V> {}
/**
* Bound mismatch: The type Integer is not a valid substitute for the
* bounded parameter <T extends V> of the type Tree<K,V>.Node<T>
*/
private final class InnerNode extends Node<Integer> {}
private final class LeafNode<T extends V> extends Node<T> {}
}
我想做甚么可能吗?或者对泛型的严重误解?
答案 0 :(得分:2)
public class Tree<K, V> {
private abstract class Node<T> extends Page<K, T> {}
private final class InnerNode extends Node<Integer> {}
private final class LeafNode extends Node<V> {}
}
这一切都非常简单和华丽 - 美丽的裸露和所有这些:D