不理解嵌套类型
我正在准备 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
非静态内部类可以访问其外部类的所有成员。就好像它实际上被定义为:
所以让我注释一下你的方法:
A non-static inner class has access to all of the members of its outer class. It's like it was really defined as:
So let me annotate your method:
我假设您正在谈论内部对象。
好吧,它们不被视为一个对象,但它们链接到同一个外部对象(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)
实例化非静态内部类需要引用“外部”类。对于
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 ofOuter
active and hence bothnew Inner()
andthis.new Inner()
refer to the sameOuter
object but are unique objects themselves. Basically, all nested non-static class instances are internally always tied to anOuter
class instance.我不明白你所说的“两个第一个对象”,但是
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 hereinner = new Outer().new Inner();
.The first Outer instance's
innerCounter
gets incremented by callingInner
constructor two times (Inner inner = new Inner();
andthis.new Inner();
), the second - just once (inner = new Outer().new Inner();
).Thus you're getting 2 and 1 as output.
上面两者是相同的,因为两者都引用当前运行的 Outer 实例。
与下面相同:
这里两个方法也引用同一个实例,因此
this
不会影响输出。both the above are same because both are refering to the currently running instance of
Outer
.Same as below:
Here too in both the method refer to the same instance so
this
is not affecting the output.我不明白你的困惑在哪里,但我会尝试解释为什么它打印 21。
请参阅内联注释
Note: objects are named for explanation sake.
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.