为了账号安全,请及时绑定邮箱和手机立即绑定

在java中,嵌套类对象可以使用封闭类方法吗?

在java中,嵌套类对象可以使用封闭类方法吗?

慕的地6264312 2022-06-30 19:01:15
我创建了一个简单的列表类。我想要做的是在 SLList 中创建一个方法来给大小一个 SLList 对象。我想递归地执行它,但是,我创建的以下 size() 方法不起作用。我知道实现它的其他方法,例如创建辅助方法。但我很好奇的是为什么我的 size() 不起作用?错误消息是“SLList.IntNode 的 size() 未定义”。为什么?既然我将嵌套的 IntMode 类设为 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 的新手,对私有和静态的东西很困惑,尤其是在涉及到类时。谢谢有人回答我。
查看完整描述

4 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

您可以通过添加一个额外的私有方法来调整它,但这并不是特别容易推理。除非绝对必要,否则我会避免这样做。


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();

    }

}


查看完整回答
反对 回复 2022-06-30
?
当年话下

TA贡献1890条经验 获得超9个赞

向 IntNode 类添加一个 size 方法,并从 SLList size 方法访问它以计算列表的整个大小。以下代码片段是不言自明的。有关嵌套类的更多信息,请参阅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());

    }

}


查看完整回答
反对 回复 2022-06-30
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

size()是一种方法SLList,不是IntNode。您可以参考内部的外部类方法IntNode,如下所示:


public class SLList {


    public class IntNode {

        ...


        public int size() {

            return SLList.this.size();

        }

    }


    ...


    public static int size() {

        ...

    }

}


查看完整回答
反对 回复 2022-06-30
?
三国纷争

TA贡献1804条经验 获得超7个赞

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

因此它不能被nested inner class IntNode.


查看完整回答
反对 回复 2022-06-30
  • 4 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号