约束:
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/