只有当外部 class 被参数化时,内部接口才会产生错误?为什么?
Inner interface in generates errors only when outer class is parameterized? Why?
抽象方法 getNextNode 生成错误,"cannot make a static reference to the non-static type Node," 但前提是 LinkedList 被参数化。如果我从 LinkedList 中删除泛型,错误就会消失。为什么?
public class LinkedList<T> {
Node head;
public LinkedList() {
head = new Node();
}
private class Node {
}
interface stuff {
public Node getNextNode();//ERROR Cannot make a static reference to the non-static type Node
}
}
正如错误试图告诉您的那样,您不能使用没有参数的泛型。
Node
其实就是LinkedList<T>.Node
。由于您的接口不是通用的(接口不从包含 class 继承类型参数),因此没有 T
它可以替代。
您可以通过使 Node
class static
来解决此问题,这样它就不会从其包含的 class.[=26 继承类型参数=]
但是,您实际上并不想这样做,因为 Node
应该 是通用的。
您实际上还需要使您的界面通用,以便它可以指定 T
。
LinkedList<T>.Node
的 Node
缩写,而您的 getNextNode()
不知道 T
是什么。
interface can't be defined in an inner class
http://www.xyzws.com/javafaq/why-an-interface-cant-be-defined-in-an-inner-class/56
public class Test{
interface stuff {
public LinkedList.Node getNextNode();
}
public class LinkedList<T> {
Node head;
public LinkedList() {
head = new Node();
}
private class Node {
}
}
抽象方法 getNextNode 生成错误,"cannot make a static reference to the non-static type Node," 但前提是 LinkedList 被参数化。如果我从 LinkedList 中删除泛型,错误就会消失。为什么?
public class LinkedList<T> {
Node head;
public LinkedList() {
head = new Node();
}
private class Node {
}
interface stuff {
public Node getNextNode();//ERROR Cannot make a static reference to the non-static type Node
}
}
正如错误试图告诉您的那样,您不能使用没有参数的泛型。
Node
其实就是LinkedList<T>.Node
。由于您的接口不是通用的(接口不从包含 class 继承类型参数),因此没有 T
它可以替代。
您可以通过使 Node
class static
来解决此问题,这样它就不会从其包含的 class.[=26 继承类型参数=]
但是,您实际上并不想这样做,因为 Node
应该 是通用的。
您实际上还需要使您的界面通用,以便它可以指定 T
。
LinkedList<T>.Node
的 Node
缩写,而您的 getNextNode()
不知道 T
是什么。
interface can't be defined in an inner class
http://www.xyzws.com/javafaq/why-an-interface-cant-be-defined-in-an-inner-class/56
public class Test{
interface stuff {
public LinkedList.Node getNextNode();
}
public class LinkedList<T> {
Node head;
public LinkedList() {
head = new Node();
}
private class Node {
}
}