java - 与外部类同名的内部类?

标签 java inner-classes

约束:

I have a maven source code generator that I wrote that is creating POJO classes from some data files that have nested namespaces. I want each namespace to be nested as an inner class. In some cases out of my control I end up with inner classes that are the same simple name as the outermost class.

All the classes must be public scope as this is for a type safe wrapper over something like a properties file, but hierarchical..

I can't change the names otherwise I am changing the names meaning and the namespace that is enclosing data.

鉴于我有以下代码:

public class A
{
    public class B
    {
        public class A
        {

        }
    }
}

内部类应该附加外部类的名称以形成一个唯一的命名空间,例如A$B$A.class,我还没有找到不编译的正当理由。

有没有什么技巧可以让它编译?

最佳答案

没有。 From the JLS section on class declarations:

It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces.

注意:我第一次通过寻找明确的规则时不知何故设法错过了这一点。如果你想要我到达这里的曲折方式,请检查编辑历史。

关于java - 与外部类同名的内部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214764/

相关文章:

java - 对象实例化 : Inner class and outer class with the same name (Java)

java - 内部静态类的新实例在 Lollipop 之前的设备中具有不同顺序的变量

java - 等待按钮按下 JButton

java - 如何在Tomcat中模拟connectionTimeout?

java - 如何对列表进行排序,并在末尾添加另一个列表中的数据?

java - GWT:链接器和代码拆分

java token if 语句

java - 如何使用 Mockito/Powermockito 在内部类中对逻辑进行单元测试

c++ - 将 C 识别的指针类型定义为内部 C++ 类

c# - 内部类对象到外部类构造函数到c#