如何完全限定其包名称与本地成员名称冲突的类? [英] How to fully qualify a class whose package name collides with a local member name?

查看:110
本文介绍了如何完全限定其包名称与本地成员名称冲突的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是供JLS专家使用的一个非常好奇的Java 7语言难题.下列代码均无法使用javac或Eclipse进行编译:

OK, here's a very curious Java 7 language puzzle for the JLS specialists out there. The following piece of code won't compile, neither with javac nor with Eclipse:

package com.example;

public class X {
    public static X com = new X();

    public void x() {
        System.out.println(com.example.X.com);
        // cannot find symbol  ^^^^^^^
    }
}

似乎成员com完全禁止从X内部访问com.*软件包.但是,这还没有完全应用.例如,下面的作品:

It appears as though the member com completely prevents access to the com.* packages from within X. This isn't thoroughly applied, however. The following works, for instance:

public void x() {
    System.out.println(com.example.X.class);
}

我的问题:

  • JLS如何证明这种行为合理?
  • 如何解决此问题

请注意,这只是对生成的代码中的实际问题的简化,其中需要完全限定com.example.X并且com成员不能重命名.

Note, this is just a simplification for a real problem in generated code, where full qualification of com.example.X is needed and the com member cannot be renamed.

更新:我认为这实际上可能是类似的问题:为什么我不能静态导入" 等于" Java中的方法?

Update: I think it may actually be a similar problem like this one: Why can't I "static import" an "equals" method in Java?

推荐答案

简单名称可能会在可能存在的上下文中出现 解释为变量,类型或包的名称.在这些 在这种情况下,第6.5节的规则指定将选择一个变量 优先于类型,并且优先选择类型 到一个包装. 因此,有时可能无法提及 可见类型或通过简单名称声明的程序包. 这样的声明被遮盖了.

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

这篇关于如何完全限定其包名称与本地成员名称冲突的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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