java 封闭类_在java中,嵌套类对象可以使用封闭类方法吗?(In java, can nested class object use the enclosing class method?)...

I created a simple list class. What I want to do is to create a method in SLList to give the size a SLList object. I want to do it recursively, however, the following size() method I created just does not work. I know other ways to realize it such as creating a helper method. But what I am curious about is that why does my size() does not work? The error message is the “size() is undefined for SLList.IntNode”. Why? Since I made the nested IntMode class just public and non-static, why it cannot use the method that is defined in SLList class?

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

}

}

I am just new to Java, and quite confused about the private and static things, especially when it comes to the Class. Thank you for anyone answering me.

have you tried? In your code, how would it make sense to do so?

– Stultuske

2019-02-22 11:22:00Z

IntNode has no method called size(). And right now there is no recursion at all. You should google for singly linked list

– XtremeBaumer

2019-02-22 11:22:27Z

@Stultuske sorry, I made some indentation mistakes on the above code. Now I corrected it. but the key problem of mine is that can IntNode variable or object use the method defined in SLLlist?

– zangsy

2019-02-22 11:26:23Z

@XtremeBaumer Do you mean that the first.next actually needs to use the method defined in IntNode? Here the size() is not defined in IntNode, so it cannot use it, right? So, does that mean the nested Class cannot use the outer Class's method?

– zangsy

2019-02-22 11:31:38Z

@zangsy again: how would that make any sense?

– Stultuske

2019-02-22 11:32:23Z

You can fiddle it by adding an extra private method but it’s not particularly easy to reason about. I would avoid doing it this way unless absolutely necessary.

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

}

}

Reason is : your method size() is in class SLList.

Hence it cannot be accessed by nested inner class IntNode.

size() is a method of SLList, not IntNode. You can refer to outer class method inside IntNode as follows:

public class SLList {

public class IntNode {

...

public int size() {

return SLList.this.size();

}

}

...

public static int size() {

...

}

}

Add a size method to IntNode class and access it from SLList size method to calculate the entire size of the list. The following code snippet is self explanatory. For more information about nested classes refer 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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值