java 访问外部类的字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6537082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:15:51  来源:igfitidea点击:

Accessing field of outer class

javafieldinner-classes

提问by fredoverflow

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.fieldand many other variants. Is there a syntax that does what I want?

我还尝试了Outer.parameter.field许多其他变体。有没有我想要的语法?

采纳答案by jtoberon

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?

乍一看,您的示例看起来更像是一个静态方法,因为它正在操纵参数的状态而不是它自己的状态。静态问题之前已经介绍过了,例如,请参阅在 Java 中,当我不在内部类中时,如何访问外部类?

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$0for its own purposes, but you do not have access to this synthetic field.

但是你的例子是一个非静态方法,我想我明白你得到了什么以及为什么你认为外部引用应该是可能的。毕竟,在其他情况下,您可以访问您自己类型的其他实例的实现细节——例如,在覆盖 equals 时引用输入参数的私有成员字段是很常见的。不幸的是,我只是不认为 java 提供了一种引用另一个词法封闭实例的方法。这可能与java 实际实现非静态内部类的方式有关。Javathis$0用于其自身目的,但您无权访问此合成字段。

回答by user1504714

How about this solution:

这个解决方案怎么样:

class Outer
{
    int field;

    class Inner
    {
        final Outer outer = Outer.this;
        void method(Inner parameter)
        {
            // working on the current instance is easy :)
            field = 0;
            Outer.this.field = 1;

            // working on another instance:
            parameter.outer.field = 2; 
        }
    }
}

回答by Ted Hopp

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.

从内部类的外部,我相信没有办法,给一个内部类实例的引用,来引用外部类实例的成员。在非静态内部类中,您当然可以使用Outer.this.*语法引用它们。

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 using Outer.this; however, that syntactic sugar is not available outside the inner class itself. Neither is the compiler-generated field.

这样想:内部类实际上是一个完全独立的类。它有一个编译器生成的字段(通常命名为类似this$0)。在内部类中,该语言允许您使用Outer.this;引用该字段。然而,这种语法糖在内部类本身之外是不可用的。编译器生成的字段也不是。