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

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

在java中,嵌套類對象可以使用封閉類方法嗎?

在java中,嵌套類對象可以使用封閉類方法嗎?

慕的地6264312 2022-06-30 19:01:15
我創(chuàng)建了一個簡單的列表類。我想要做的是在 SLList 中創(chuàng)建一個方法來給大小一個 SLList 對象。我想遞歸地執(zhí)行它,但是,我創(chuàng)建的以下 size() 方法不起作用。我知道實現(xiàn)它的其他方法,例如創(chuàng)建輔助方法。但我很好奇的是為什么我的 size() 不起作用?錯誤消息是“SLList.IntNode 的 size() 未定義”。為什么?既然我將嵌套的 IntMode 類設(shè)為 public 和 non-static,為什么它不能使用 SLList 類中定義的方法?public class SLList {    public class IntNode {        public int item;        public IntNode next;        public IntNode(int i, IntNode n) {            item = i;            next = n;        }    }    private IntNode first;    public SLList(int x) {        first = new IntNode(x, null);    }    public int size() {        if (first.next == null) {           return 1;        }        return 1 + first.next.size();    }}我只是 Java 的新手,對私有和靜態(tài)的東西很困惑,尤其是在涉及到類時。謝謝有人回答我。
查看完整描述

4 回答

?
守著星空守著你

TA貢獻1799條經(jīng)驗 獲得超8個贊

您可以通過添加一個額外的私有方法來調(diào)整它,但這并不是特別容易推理。除非絕對必要,否則我會避免這樣做。


class SLList {


    public class IntNode {


        public int item;

        public IntNode next;


        public IntNode(int i, IntNode n) {

            item = i;

            next = n;

        }


        private int theSize()

        {

            return size();

        }

    }


    private IntNode first;


    public SLList(int x) {

        first = new IntNode(x, null);

    }


    public int size() {

        if (first.next == null) {

            return 1;

        }

        return 1 + first.next.theSize();

    }

}


查看完整回答
反對 回復(fù) 2022-06-30
?
當(dāng)年話下

TA貢獻1890條經(jīng)驗 獲得超9個贊

向 IntNode 類添加一個 size 方法,并從 SLList size 方法訪問它以計算列表的整個大小。以下代碼片段是不言自明的。有關(guān)嵌套類的更多信息,請參閱https://www.programiz.com/java-programming/nested-inner-class


public class SLList {


    public class IntNode {


        public int item;

        public IntNode next;


        public IntNode(int i, IntNode n) {

            item = i;

            next = n;

        }


        public int size() {

            IntNode tmp = next;


            if (tmp == null) {

                return 1;

            }


            return 1 + tmp.size();

        }

    }


    private IntNode first;


    public SLList(int x) {

        first = new IntNode(x, null);

    }


    public int size() {

        if (first == null)

            return 0;

        return first.size();

    }


    public static void main(String[] args) {

        SLList list = new SLList(10);

        list.first.next = list.new IntNode(20, null);

        list.first.next.next = list.new IntNode(30, null);

        list.first.next.next.next = list.new IntNode(40, null);


        System.out.println(list.size());

    }

}


查看完整回答
反對 回復(fù) 2022-06-30
?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

size()是一種方法SLList,不是IntNode。您可以參考內(nèi)部的外部類方法IntNode,如下所示:


public class SLList {


    public class IntNode {

        ...


        public int size() {

            return SLList.this.size();

        }

    }


    ...


    public static int size() {

        ...

    }

}


查看完整回答
反對 回復(fù) 2022-06-30
?
三國紛爭

TA貢獻1804條經(jīng)驗 獲得超7個贊

原因是:您的方法size()SLList類中。

因此它不能被nested inner class IntNode.


查看完整回答
反對 回復(fù) 2022-06-30
  • 4 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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