1 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個贊
您在外部類本身內(nèi)部使用靜態(tài)類,因此您不要放置封閉類名稱。靜態(tài)嵌套類在行為上類似于任何靜態(tài)字段。
但是如果要在外部類之外實(shí)例化靜態(tài)嵌套類,則必須在其定義上放置封閉類名稱或使用對外部類的引用。
例如 :
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;
}
}
}
添加回答
舉報