与外部类同名的内部类? [英] Inner classes with the same name as an outer class?

查看:90
本文介绍了与外部类同名的内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个写的用于创建POJO类的Maven源代码生成器 来自具有嵌套名称空间的某些数据文件.我希望每个名称空间 被嵌套为内部类.在某些情况下,我最终无法控制 具有与最外层相同的简单名称的内部类 课.

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.

所有类都必须在public范围内,因为这是类型安全的 包装在诸如属性文件之类的东西上,但是是分层的.

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,但我没有找到不进行编译的正当理由.

Inner classes should append the name of the outer class to form a unique namespace such as A$B$A.class, I haven't found a valid reason for this not to compile.

是否有任何技巧可以使其编译?

推荐答案

否. 在有关类声明的JLS部分中:

如果一个类与其任何包含的类或接口具有相同的简单名称,则是编译时错误.

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

注意:在寻找明确规则的第一遍中,我以某种方式设法错过了这一点.如果您想要我到达这里的曲折方式,请查看编辑历史记录.

这篇关于与外部类同名的内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆