Java 内部类泛型-不兼容类型
我似乎不明白为什么这些类型是不兼容的,是不是因为它们在不同的类级别上?我就是不太明白为什么Java 内部类泛型-不兼容类型,java,generics,incompatibletypeerror,Java,Generics,Incompatibletypeerror,我似乎不明白为什么这些类型是不兼容的,是不是因为它们在不同的类级别上?我就是不太明白为什么 public class PrependLinearListImpl<T>{ private Node<T> first; private class Node<T> { private T head; private Node<T> tail; Node(T head) {
public class PrependLinearListImpl<T>{
private Node<T> first;
private class Node<T> {
private T head;
private Node<T> tail;
Node(T head) {
this.head = head;
this.tail = first;
first = this;
}
}
}
公共类prependlinearListPL{
私有节点优先;
私有类节点{
私人T型头;
私有节点尾部;
节点(T头){
这个头=头;
this.tail=第一;
第一个=这个;
}
}
}
这是因为prependlinearListPL.Node
已经从其外部类继承了泛型参数。没有必要重新定义泛型组件
以下各项应按原样工作:
public class PrependLinearListImpl<T>{
private Node first;
private class Node {
private T head;
private Node tail;
Node(T head) {
this.head = head;
this.tail = first;
first = this;
}
}
}
公共类prependlinearListPL{
私有节点优先;
私有类节点{
私人T型头;
私有节点尾部;
节点(T头){
这个头=头;
this.tail=第一;
第一个=这个;
}
}
}
如果
节点
是静态的
,则有必要提供它自己的泛型参数。这是因为PrependlinearListPL.Node
已经从它的外部类继承了泛型参数。没有必要重新定义泛型组件
以下各项应按原样工作:
public class PrependLinearListImpl<T>{
private Node first;
private class Node {
private T head;
private Node tail;
Node(T head) {
this.head = head;
this.tail = first;
first = this;
}
}
}
公共类prependlinearListPL{
私有节点优先;
私有类节点{
私人T型头;
私有节点尾部;
节点(T头){
这个头=头;
this.tail=第一;
第一个=这个;
}
}
}
如果
Node
是static
,那么就必须提供它自己的泛型参数。确切的错误是什么?你有两个不同的T
s。嗨,不知何故,我认为内部类T与外部类T完全相同,我是否应该将其重命名为“E”并将其扩展为“T”?错误如下:类型不兼容。必需:PrependlinearListPL.Node发现:PrependlinearListPL.Node确切错误是什么?您有两个不同的T
s.Hi,不知何故,我认为内部类T与外部类T完全相同,我是否应该将其重命名为“E”并将其作为“T”的扩展?错误如下:类型不兼容。必需:找到PrependlinearListPL.Node:PrependlinearListPL.Node