内部类访问外部类方法和属性的方式有区别吗?

发布于 2025-01-17 21:51:50 字数 1050 浏览 1 评论 0原文

我遇到了两种方法:

  1. 在外部类的代码中,我们创建(通过 new 关键字)内部类,我们可以向内部类构造函数发送一个外部类的实例(使用 this 关键字)。 这种方式可以在 ArrayList 类实现 subList 方法中找到,代码如下:
public List<E> subList(int fromIndex, int toIndex) {
    subListRangeCheck(fromIndex, toIndex, size);
    return new SubList(this, 0, fromIndex, toIndex);
}

SubList(AbstractList<E> parent,
        int offset, int fromIndex, int toIndex) {
    this.parent = parent;
    this.parentOffset = fromIndex;
    this.offset = offset + fromIndex;
    this.size = toIndex - fromIndex;
    this.modCount = ArrayList.this.modCount;
}
  1. 不发送 this 关键字,然后在内部类中我们想要访问 ArrayList 的任何地方(外部类)方法和属性我们可以编写以下代码:
ArrayList.this.arrayListMethod()

而不是编写:

parent.arrayListMethod()

其中parent是对通过this关键字传递的ArrayList类的引用

我的问题是,将两种方法都有效(如果答案是肯定的,哪一种是 更好的)?

我对这个问题的发布方式表示歉意,但我未能在代码部分插入代码。如果有人可以编辑它并使其更具可读性,我会很高兴,因为我在这次任务中失败了。

I have encountered two ways:

  1. Where in the code of the Outer class we are creating (by new keyword) the Inner class we can send to the Inner class constructor an instance to his Outer class (using this keywords).
    This way can be found in ArrayList class implementation of subList method with the code:
public List<E> subList(int fromIndex, int toIndex) {
    subListRangeCheck(fromIndex, toIndex, size);
    return new SubList(this, 0, fromIndex, toIndex);
}

SubList(AbstractList<E> parent,
        int offset, int fromIndex, int toIndex) {
    this.parent = parent;
    this.parentOffset = fromIndex;
    this.offset = offset + fromIndex;
    this.size = toIndex - fromIndex;
    this.modCount = ArrayList.this.modCount;
}
  1. Not sending this keyword and then in any place in the Inner class where we want to access ArrayList (outer class) method and properties we can write the following code:
ArrayList.this.arrayListMethod()

instead of writing:

parent.arrayListMethod()

Where parent is the reference to the ArrayList class that was passed by this keyword

My question, will both ways work (if the answer is yes, which one is better)?

I'm apologize the way this question was post, but i have failed in inserting the code under code section. I will be glad if someone can edit it and make it more readable cause i failed in this mission.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2025-01-24 21:51:50

Ways to create nested classes in java

  1. class inside interface
  2. class inside function
  3. class inside class
  4. static class inside class

here we are talking about class inside class means inner class should not exist if there is no object of outer class example Map contain Entity class
没有地图对象,就不应该有任何实体。

让我们提出质疑,这两个都在做同样的工作,但是,使用父不合适,因为如果InnerClass不应使用参考变量包含任何参考外call。

class Outer{
 class Inner{
  Outer parent;
 }
}

了解使用className的需求。以下


public class A {

    int task(){
        return 0;
    }

    int task3(){
        return 0;
    }

    class Event{

        int task(){
            /// need to use class name because function name are same
            return A.this.task();
        }

        int task2(){
            /// can directly use outer class object
            return task3();
        }
    
    }

}

///     Event obj = new A().new Event();  // to create obj of inner class

Ways to create nested classes in java

  1. class inside interface
  2. class inside function
  3. class inside class
  4. static class inside class

here we are talking about class inside class means inner class should not exist if there is no object of outer class example Map contain Entity class
without map object there should not be any entity.

Let's come to question, Both the this are doing same work however, using parent is not suitable Because if InnerClass should not contain any reference OuterCall using reference variable.

class Outer{
 class Inner{
  Outer parent;
 }
}

To understand the need of using className.this as follows


public class A {

    int task(){
        return 0;
    }

    int task3(){
        return 0;
    }

    class Event{

        int task(){
            /// need to use class name because function name are same
            return A.this.task();
        }

        int task2(){
            /// can directly use outer class object
            return task3();
        }
    
    }

}

///     Event obj = new A().new Event();  // to create obj of inner class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文