【问题标题】:In java, can nested class object use the enclosing class method?在java中,嵌套类对象可以使用封闭类方法吗?
【发布时间】:2019-02-22 11:20:37
【问题描述】:

我创建了一个简单的列表类。我想要做的是在 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,对私有和静态的东西很困惑,尤其是在涉及到类时。谢谢大家回复我。

【问题讨论】:

  • 你试过了吗?在您的代码中,这样做有什么意义?
  • IntNode 没有名为 size() 的方法。现在根本没有递归。你应该用谷歌搜索singly linked list
  • @Stultuske 抱歉,我在上面的代码中犯了一些缩进错误。现在我纠正了它。但是我的关键问题是IntNode变量或对象可以使用SLLlist中定义的方法吗?
  • @XtremeBaumer 你的意思是first.next实际上需要使用IntNode中定义的方法吗?这里的 size() 没有在 IntNode 中定义,所以它不能使用它,对吧?那么,这是否意味着嵌套类不能使用外部类的方法呢?
  • @zangsy 再次:这有什么意义?

标签: java static inner-classes


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    因此nested inner classIntNode无法访问。

    【讨论】:

      【解决方案3】:

      size()SLList 的方法,而不是IntNode。可以参考IntNode里面的外部类方法如下:

      public class SLList {
      
          public class IntNode {
              ...
      
              public int size() {
                  return SLList.this.size();
              }
          }
      
          ...
      
          public static int size() {
              ...
          }
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2021-11-06
          • 2013-09-18
          • 2011-01-14
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 2018-04-17
          相关资源
          最近更新 更多