本文介绍了静态内部类需要导入注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我做了一些JUnit的测试和想写不同的类,也有类似的功能,但足以小到一个类中编写。无论设计决定它给我带来了一个编译器错误,我不知道规则是什么什么,我所看到的。

So I was doing some jUnit testing and wanted to write distinct classes that had similar functionality but were small enough to write within a single class. Regardless of the decision for design it brought me to a compiler error I am not sure what the rules are for what I saw.

您可以想像它会看起来像

You can imagine it would look something like

package foo;

@RunWith(Suite.class)
@SuiteClasses({ TestClassOne.class, TestClassTwo.class })
public class TestSuite{

   @RunWith(SpringJUnit4ClassRunner.class)
   public static class TestClassOne{

   }

   @RunWith(SpringJUnit4ClassRunner.class)
   public static class TestClassTwo{

   }
}

现在当编译器踢它,它会说的 TestClassOne不能被解析为一个类型的。还有就是要解决这个一个简单的方法。这将需要实例的静态类的露骨进口。

Now when the compiler kicks it it will say TestClassOne cannot be resolved to a type. There is an easy way to resolve this. It would require an explict import of the static class for instance.

import foo.TestSuite.TestClassOne;
import foo.TestSuite.TestClassTwo;

我的问题是,任何人都可以解释一下编译器的规则或原因可能有注释不能够看到类的静态内部类。请记住一个包私有类被认为是罚款,没有进口编译。

My question is, can anyone explain what compiler rules or reasons there may be for the annotations to not be able to see the class static inner class. Keep in mind a package private class is seen fine and compiles without an import.

推荐答案

这是一个有趣的。根据文献[1],名称的范围TestClassOne是的 的类的TestSuite的整个身体。

This is an interesting one. According to [1], the scope of the name "TestClassOne" is "the entire body of" class "TestSuite".

在注解的 的TestSuite的身体?显然不是。但是,这不是很公平。引入注释之前范围规则定义。我看不出任何问题,如果一个类的注释在类的考虑范围之内。他们是非常亲密的反正。

Is the annotation in "the body of" TestSuite? Apparently not. But that's not very fair. The scope rule was defined before annotation was introduced. I don't see any problem if a class annotation is considered in the scope of the class. They are very intimate anyway.

另一个问题是怎么来的简单的名称的TestSuite可以在注释中引用?原来,该规范涵盖了这一项。注释是一个修饰符,它的类型是声明的一部分,而顶级类型的范围是所有的类型声明的包中。

Another question is how come simple name "TestSuite" can be referenced in the annotation? It turns out the spec covers this one. Annotation is a modifier, which is part of the type declaration, and "The scope of a top level type is all type declarations in the package".

然而,它是可能的,该规范得到它的权利偶然。这些规则被定义引入注释之前,并保持不变之后。因此,尽管它覆盖在技术性的情况下,它可能是一个意外。这不是怀疑语言设计者的脑力 - 全规格是太可恶复杂

However it is possible that the spec got it right by accident. The rules were defined before annotation was introduced, and remain the same afterwards. So although it covers the case in technicality, it could be an accident. This is not to doubt the brain power of language designers - the whole spec is just too damn complex.

[1] http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3

这篇关于静态内部类需要导入注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 20:50