java Java内部类的两个问题(class A { class B { } })

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/339245/
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-29 12:01:19  来源:igfitidea点击:

Two questions on inner classes in Java (class A { class B { } })

javaclass

提问by balu

Sorry for the bad title, but I couldn't think of a better one.

抱歉标题不好,但我想不出更好的标题。

I'm having a class A and a class B which is kind of a sub class of A, like so:

我有一个 A 类和一个 B 类,它是 A 的一个子类,如下所示:

(Is there actually a correct name for it? Isn't "sub class" reserved for inheritance?)

(它实际上有正确的名称吗?“子类”不是为继承保留的吗?)

class A {
    int i = 0;
    class B {
        int j = 1;
    }
}

class Test {
    public static void main() {
        A a = new A();
        B b = a.new B();
        A c = ??? b ??? // get "a" back
    }
}

From B every property of A can be accessed, therefore both, a.i and b.i, return 0. Now, I'm wondering whether it's somehow possible to retrieve the original object of type A out of b, as b contains everything that a contains? Simple casting apparently doesn't do the trick.

从 B 可以访问 A 的每个属性,因此 ai 和 bi 都返回 0。现在,我想知道是否有可能从 b 中检索类型 A 的原始对象,因为 b 包含 a 包含的所有内容?简单的铸造显然不能解决问题。

Second one:

第二个:

class A {

    void print() {
        System.out.println("This is class A.");
    }

    class B {
        void print() {
            // <--- How to access print() of class A (like this.A.print() or smth)? 
            System.out.println("This is class B.");
        }
    }
}

You could alternatively also provide me with some good resources on this topic, as I've been too stupid to find a good one so far.

您也可以为我提供有关此主题的一些很好的资源,因为到目前为止我太愚蠢而找不到好的资源。

Thanks in advance. :)

提前致谢。:)

回答by Johannes Schaub - litb

There doesn't seem to be a way to access the outer class from outside. But you can do it like this:

似乎没有办法从外部访问外部类。但是你可以这样做:

class A {
    int i = 0;
    class B {
        final A outer = A.this;
        int j = 1;
    }
}

class Test {
    public static void main() {
        A a = new A();
        A.B b = a.new B();
        A c = b.outer // get "a" back
    }
}

ClassName.thiswill be the instance of the outerclass associated with the instance of an inner class.

ClassName.this将是与内部类的实例相关联的外部类的实例。

回答by madlep

You can access it with the ParentClass.thissyntax from within the inner class.

您可以使用ParentClass.this内部类中的语法访问它。

e.g.

例如

public class Outter
{
    class Inner {
        public Outter getOutter()
        {
            return Outter.this;
        }
    }

    public Inner getInner(){
        return new Inner();
    }
}

class Runner{
    public static void main(String[] args){
        Outter out = new Outter(); 
        Outter.Inner inner = out.getInner();

        System.out.println(inner.getOutter().toString());
    }
}

回答by Juliet

[Edit: My answer is appropriate for C# programmers, but I can't guarantee that its applicable to Java.]

[编辑:我的回答适用于 C# 程序员,但我不能保证它适用于 Java。]

B is an inner class, not a subclass of A. Additionally, B does not hold an instance of A, so your code as iscannot return any instance of A.

B是一个内部类,而不是A的一个子类。此外,B不举行的一个实例,因此你的代码为是无法返回A的任何实例

You need to restructure your classes as follows:

您需要按如下方式重组您的类:

class A
{
    public class B
    {
       public A Parent;
       public B(A parent)
       {
          this.Parent = parent;
       }
    }
}

Now your B class has a field 'Parent' which returns its parent. You can use these classes as follows (this is C# syntax, because I don't know if Java has a different syntax for instantiating inner classes):

现在你的 B 类有一个字段 'Parent' 返回它的父级。您可以按如下方式使用这些类(这是C#语法,因为我不知道Java是否有不同的实例化内部类的语法):

public static void Main(String[] args)
{
    A parent = new A();
    A.B child = new A.B(child);
    A backToParent = child.Parent;
}

Of course, creating your B class in this way seems little funny: technically, you can pass in any parent. It would probably be better to rewrite your A class with a method which returns a B:

当然,以这种方式创建 B 类似乎没什么可笑的:从技术上讲,您可以传入任何父级。使用返回 B 的方法重写 A 类可能会更好:

class A
{        
    public class B
    {
       public A Parent;
       public B(A parent)
       {
          this.Parent = parent;
       }
    }

    public B getChild()
    {
        return new B(this);
    }
}

public static void Main(String[] args)
{
    A parent = new A();
    A.B child = A.getChild();
    A backToParent = child.Parent;
}

回答by newacct

this seemed to work for me

这似乎对我有用

class A {
    int i = 0;
    class B {
        int j = 1;
    }
}

class Test {
    public static void main() {
        A a = new A();
        A.B b = a.new B();
        A c = (A)b.getClass().getDeclaredField("this##代码##").get(b);
    }
}