第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

靜態(tài)類 Object 是如何在沒有引用的情況下創(chuàng)建的?

靜態(tài)類 Object 是如何在沒有引用的情況下創(chuàng)建的?

一只甜甜圈 2022-06-23 20:25:15
我試圖通過Java解決LinkedList的問題,但我發(fā)現(xiàn)了靜態(tài)內(nèi)部類的概念,我被困在這里!我的代碼正在運(yùn)行,但無法理解如何創(chuàng)建靜態(tài)類對象public class findNthNodeInLL {    static class Node {       int data;       Node next;       Node(int data) {          this.data = data;          next = null;       }    }int findNthNode(Node head, int count) {    int pos = 0;    Node ptr = head;    while(ptr != null && pos != count) {        pos++;        ptr = ptr.next;    }    return ptr.data;}public static void main(String[] args) {    findNthNodeInLL ll = new findNthNodeInLL();    Node head = new Node(1);    head.next = new Node(2);    head.next.next = new Node(3);    head.next.next.next = new Node(4);    head.next.next.next.next = new Node(5);    System.out.println(ll.findNthNode(head,3));}}內(nèi)部類對象(即頭部)在沒有任何外部類引用的情況下被創(chuàng)建。甚至正在調(diào)用構(gòu)造函數(shù)并且正在創(chuàng)建內(nèi)存而沒有任何外部類引用。這里的實(shí)際情況是什么?怎么了?為什么我們不對內(nèi)部類構(gòu)造函數(shù)或?qū)ο笫褂萌魏瓮獠款愐茫恳苍S我錯過了一些東西。請幫助我了解這里的情況。
查看完整描述

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;

    }

}

}


查看完整回答
反對 回復(fù) 2022-06-23
  • 1 回答
  • 0 關(guān)注
  • 92 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號