简体   繁体   English

如何访问由外部类的成员隐藏的嵌套类的成员

[英]How to access a member of a nested class, that is hidden by a member of the outer class

I have a source-code generator that risks generating the following type of code (just an example): 我有一个源代码生成器,有可能生成以下类型的代码(只是一个例子):

public class Outer {
    public static final Object Inner = new Object();

    public static class Inner {
        public static final Object Help = new Object();
    }

    public static void main(String[] args) {
        System.out.println(Outer.Inner.Help);
        //                             ^^^^ Cannot access Help
    }
}

In the above example, Inner is ambiguously defined inside of Outer . 在上面的例子中, InnerOuter被模糊地定义。 Outer.Inner can be both a nested class, and a static member. Outer.Inner既可以是嵌套类,也可以是静态成员。 It seems as though both javac and Eclipse compilers cannot dereference Outer.Inner.Help . 似乎javac和Eclipse编译器都无法取消引用Outer.Inner.Help How can I access Help ? 我如何获得Help

Remember, the above code is generated, so renaming things is not a (simple) option. 请记住,上面的代码是生成的,因此重命名事物不是(简单)选项。

The following works for me (with a warning about accessing static members in a non-static way): 以下对我有用(带有关于以非静态方式访问静态成员的警告):

public static void main(String[] args) {
    System.out.println(((Inner)null).Help);
}

How about 怎么样

public static void main(String[] args) {
    System.out.println(new Inner().Help);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM