1 回答

TA貢獻1802條經驗 獲得超5個贊
您在外部類本身內部使用靜態類,因此您不要放置封閉類名稱。靜態嵌套類在行為上類似于任何靜態字段。
但是如果要在外部類之外實例化靜態嵌套類,則必須在其定義上放置封閉類名稱或使用對外部類的引用。
例如 :
public class Main {
static class NodeInside {
int data;
NodeX.Node next;
NodeInside(int data) {
this.data = data;
next = null;
}
}
public static void main(String[] args) {
NodeX ll = new NodeX();
NodeX.Node head = new NodeX.Node(1); // need to put the enclosing class name
NodeInside nodeInside = new NodeInside(1); // no need to put the enclosing class
}
}
class NodeX{
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
}
添加回答
舉報