不理解嵌套类型

发布于 2024-12-21 15:00:38 字数 753 浏览 5 评论 0原文

我正在准备 SCJP,在阅读有关嵌套类型的内容时我感到很困惑。有一个简单的问题,我无法理解结果。

public class Outer {
    private int innerCounter;

    class Inner {
        Inner() {
            innerCounter++;
        }

        public String toString() {
            return String.valueOf(innerCounter);
        }
    }

    private void multiply() {
        Inner inner = new Inner();
        this.new Inner();
        System.out.print(inner);
        inner = new Outer().new Inner();
        System.out.println(inner);
    }

    public static void main(String[] args) {
        new Outer().multiply();
    }
}

它打印

21

Knowing that the counter is not static, how are the front of the four object recognizes as one object?

I am preparing for SCJP and I got confused while reading about nested types. There is a simple question and I am not able to understand the result.

public class Outer {
    private int innerCounter;

    class Inner {
        Inner() {
            innerCounter++;
        }

        public String toString() {
            return String.valueOf(innerCounter);
        }
    }

    private void multiply() {
        Inner inner = new Inner();
        this.new Inner();
        System.out.print(inner);
        inner = new Outer().new Inner();
        System.out.println(inner);
    }

    public static void main(String[] args) {
        new Outer().multiply();
    }
}

It prints

21

Knowing that the counter is not static, how are the first two objects considered as one object?

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

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

发布评论

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

评论(6

还不是爱你 2024-12-28 15:00:38

非静态内部类可以访问其外部类的所有成员。就好像它实际上被定义为:

class Outer {

    private int innerCount;

    class Inner() {
        private final Outer owner;

        public Inner(Outer owner) {
            this.owner = owner;
            this.owner.innerCount++;
        }
    }

    private void multiply() {
        Inner inner = new Inner(this);
        ...
    }
}

所以让我注释一下你的方法:

private void multiply() {
    // this.innerCount = 0

    Inner inner = new Inner();
    // this.innerCount = 1

    this.new Inner();
    // this.innerCount = 2

    System.out.print(inner);  // Prints "2"

    // Creates a new Outer (with a separate innerCount)
    // then uses that to create a new Inner, which updates
    // the new innerCount
    inner = new Outer().new Inner();
    // inner.innerCount = 1

    System.out.println(inner);  // Prints "1"
}

A non-static inner class has access to all of the members of its outer class. It's like it was really defined as:

class Outer {

    private int innerCount;

    class Inner() {
        private final Outer owner;

        public Inner(Outer owner) {
            this.owner = owner;
            this.owner.innerCount++;
        }
    }

    private void multiply() {
        Inner inner = new Inner(this);
        ...
    }
}

So let me annotate your method:

private void multiply() {
    // this.innerCount = 0

    Inner inner = new Inner();
    // this.innerCount = 1

    this.new Inner();
    // this.innerCount = 2

    System.out.print(inner);  // Prints "2"

    // Creates a new Outer (with a separate innerCount)
    // then uses that to create a new Inner, which updates
    // the new innerCount
    inner = new Outer().new Inner();
    // inner.innerCount = 1

    System.out.println(inner);  // Prints "1"
}
如梦初醒的夏天 2024-12-28 15:00:38

我假设您正在谈论内部对象。
好吧,它们不被视为一个对象,但它们链接到同一个外部对象(this)

I assume that you're talking about the Inner objects.
Well they're not considered as one object but they're linked to the same Outer object (this)

雨落□心尘 2024-12-28 15:00:38

实例化非静态内部类需要引用“外部”类。对于 multiply 方法,您已经有一个活跃的 Outer 实例,因此 new Inner()this 都有。 new Inner() 引用相同的 Outer 对象,但它们本身是唯一的对象。基本上,所有嵌套的非静态类实例在内部总是绑定到Outer 类实例。

Instantiating a non-static inner class requires a reference to the "outer" class. In the case of the method multiply, you already have a instance of Outer active and hence both new Inner() and this.new Inner() refer to the same Outer object but are unique objects themselves. Basically, all nested non-static class instances are internally always tied to an Outer class instance.

旧竹 2024-12-28 15:00:38

我不明白你所说的“两个第一个对象”,但是
innerCounter 是外部类的一个字段。

在代码中,您创建了两个 Outer 实例 - 这里 new Outer().multiply(); 和这里 inner = new Outer().new Inner();

第一个 Outer 实例的 innerCounter 通过调用 Inner 构造函数两次(Inner inner = new Inner();this.new Inner();),第二个 - 仅一次 (inner = new Outer().new Inner();)。

因此,您将得到 2 和 1 作为输出。

I didn't get what you were referring to as 'two first objects', but
innerCounter is a field of an Outer class.

In your code you create two instances of Outer - here new Outer().multiply(); and here inner = new Outer().new Inner();.

The first Outer instance's innerCounter gets incremented by calling Inner constructor two times (Inner inner = new Inner(); and this.new Inner();), the second - just once (inner = new Outer().new Inner();).

Thus you're getting 2 and 1 as output.

香草可樂 2024-12-28 15:00:38
Inner inner = new Inner();
Inner inner = this.new Inner();

上面两者是相同的,因为两者都引用当前运行的 Outer 实例。
与下面相同:

private int number;

public int getNumber(){
    return this.number;
}

public int getNumber(){
    return number;
}

这里两个方法也引用同一个实例,因此 this 不会影响输出。

Inner inner = new Inner();
Inner inner = this.new Inner();

both the above are same because both are refering to the currently running instance of Outer.
Same as below:

private int number;

public int getNumber(){
    return this.number;
}

public int getNumber(){
    return number;
}

Here too in both the method refer to the same instance so this is not affecting the output.

眼泪也成诗 2024-12-28 15:00:38

我不明白你的困惑在哪里,但我会尝试解释为什么它打印 21。
请参阅内联注释

Note: objects are named for explanation sake.


public class Outer {
private int innerCounter;

类 Inner { 内部(){ 内部计数器++; }

public String toString() {
    return String.valueOf(innerCounter);
}

}

私人无效乘法(){
Inner inner = new Inner();//Step2: 创建第一个内部对象
//我们称之为innerA
//注意innerA是使用outerA创建的
//增加outerA的innerCount变量
this.new Inner(); //step3 创建第二个内部对象
//我们称之为innerB
//注意innerB是使用outerA创建的
//增加outerA的innerCount变量

System.out.print(inner); //step 3 print innerCount from outerA
//With the two increments done, prints 2
inner = new Outer().new Inner(); //step4 create second outer object
//Lets call it outerB.
//Lets call this third inner object as InnerC
//InnerC creation increments OuterB's innerCount variable (so value 1)
//Assigned now to the old reference
System.out.println(inner); // prints the value of innerC

}

public static void main(String[] args) {
new Outer().multiply(); //Step1:创建第一个外部对象
//我们称之为OuterA

}
}

I don't understand where your confusion is, but I'll try and explain why it prints 21.
Please see the inline comments

Note: objects are named for explanation sake.


public class Outer {
private int innerCounter;

class Inner { Inner() { innerCounter++; }

public String toString() {
    return String.valueOf(innerCounter);
}

}

private void multiply() {
Inner inner = new Inner();//Step2: Create first inner object
//Lets call it innerA
//Note innerA is created using outerA
//Increments the innerCount variable of outerA
this.new Inner(); //step3 create second inner object
//Lets call it innerB
//Note innerB is created using outerA
//Increments the innerCount variable of outerA

System.out.print(inner); //step 3 print innerCount from outerA
//With the two increments done, prints 2
inner = new Outer().new Inner(); //step4 create second outer object
//Lets call it outerB.
//Lets call this third inner object as InnerC
//InnerC creation increments OuterB's innerCount variable (so value 1)
//Assigned now to the old reference
System.out.println(inner); // prints the value of innerC

}

public static void main(String[] args) {
new Outer().multiply(); //Step1: Create the first outer object
//Lets call it OuterA

}
}

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