访问外部类的字段
给定内部类对象的引用,如何访问外部类的字段?
class Outer
{
int field;
class Inner
{
void method(Inner parameter)
{
// working on the current instance is easy :)
field = 0;
Outer.this.field = 1;
// working on another instance is hard :(
parameter.field = 2; // does not compile
parameter.Outer.this.field = 3; // does not compile
parameter.outer().field = 4; // This works...
}
// ...but I really don't want to have to write this method!
Outer outer()
{
return Outer.this;
}
}
}
我还尝试了 Outer.parameter.field 和许多其他变体。有没有一种语法可以满足我的要求?
How do I access a field of an outer class, given a reference to an object of the inner class?
class Outer
{
int field;
class Inner
{
void method(Inner parameter)
{
// working on the current instance is easy :)
field = 0;
Outer.this.field = 1;
// working on another instance is hard :(
parameter.field = 2; // does not compile
parameter.Outer.this.field = 3; // does not compile
parameter.outer().field = 4; // This works...
}
// ...but I really don't want to have to write this method!
Outer outer()
{
return Outer.this;
}
}
}
I also tried Outer.parameter.field
and many other variants. Is there a syntax that does what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个解决方案怎么样:
How about this solution:
从内部类的外部,我相信在给出对内部类实例的引用的情况下,没有办法引用外部类实例的成员。当然,您可以在非静态内部类内部使用
Outer.this.*
语法引用它们。这样想:内部类实际上是一个完全独立的类。它有一个编译器生成的字段(通常命名为奇怪的东西,如
this$0
)。在内部类中,该语言允许您使用 Outer.this 引用该字段;但是,该语法糖在内部类本身之外不可用。编译器生成的字段也不是。From outside the inner class, I believe that there's no way, given a reference to an inner class instance, to reference members of the outer class instance. From inside a non-static inner class, you can, of course, reference them using the
Outer.this.*
syntax.Think of it this way: the inner class is actually a completely separate class. It has a compiler-generated field (usually named something weird like
this$0
). Within the inner class, the language allows you to reference that field usingOuter.this
; however, that syntactic sugar is not available outside the inner class itself. Neither is the compiler-generated field.有两种方法可以查看您正在执行的操作:静态方法或非静态方法。
乍一看,您的示例看起来更像是静态方法,因为它正在操纵参数的状态而不是其自身的状态。静态问题之前已经介绍过,例如参见 在Java中,当我不在内部类中时如何访问外部类?
但是你的例子是一个非静态方法,我想我明白了您得到了什么以及为什么您认为外部应该可以参考。毕竟,在其他情况下,您可以访问自己类型的其他实例的实现细节——例如,在重写 equals 时引用输入参数的私有成员字段是很常见的。不幸的是,我只是不认为 java 提供了一种引用另一个词法封闭实例的方法。这可能与java实际实现非静态的方式有关内部类。 Java 将
this$0
用于其自身目的,但您无权访问此合成字段。There are two ways to look at what you're doing, as a static method or as a non-static method.
At first glance, your example looks more like a static method, since it is manipulating the state of the parameter rather than its own state. The static problem has been covered before, e.g. see In Java, how do I access the outer class when I'm not in the inner class?
But your example is of a non-static method, and I think I see what you're getting and why you think that an outer reference should be possible. After all, there are other situations in which you can access implementation details of other instances of your own type -- for example, it's common to reference the input parameter's private member fields when overriding equals. Unfortunately I just don't think that java provides a way to refer to another's lexically enclosing instance. This probably has something to do with the way that java actually implements non-static inner classes. Java uses
this$0
for its own purposes, but you do not have access to this synthetic field.