[英]Why do I have to import a nested class in the class it is defined?
In the following example the import
is necessary, otherwise Java's compiler will complain that Nested
cannot be resolved to a type in Iterable<Nested>
: 在下面的示例中,
import
是必要的,否则Java的编译器会抱怨Nested
无法解析为Iterable<Nested>
中的Iterable<Nested>
:
package test;
import test.Example.Nested;
public class Example implements Iterable<Nested> {
public final static class Nested {}
}
(using Iterable<Example.Nested>
instead of an import works as well) (使用
Iterable<Example.Nested>
而不是导入工作)
This only happens when referencing a nested class in the definition of the outer class, eg when using it as a parametric type, but also when extending/implementing it (which will result in another error once the compiler can resolve the class), or when using it as or in an annotation. 这仅在引用外部类定义中的嵌套类时发生,例如,当将其用作参数类型时,以及扩展/实现它时(一旦编译器可以解析类,将导致另一个错误),或者将其用作注释或在注释中使用。
My question is: Why can't the compiler find the nested class without an explicit declaration? 我的问题是: 为什么编译器在没有显式声明的情况下找不到嵌套类?
A declaration is visible in the scope it occurs in, and for members, that scope is the stuff between the enclosing curly brackets, or as the spec puts it: 声明在它出现的范围内是可见的,对于成员,该范围是封闭的大括号之间的内容,或者如规范所示 :
The scope of a declaration of a member
m
declared in or inherited by a class typeC
(§8.1.6) is the entire body ofC
, including any nested type declarations.的成员的声明的范围
m
中声明或由类继承的类型C
(§8.1.6)是整个身体C
,包括任何嵌套类型声明。
Why it has been defined that way is something only the creators of Java can answer with any degree of certainty, but I suspect they wanted as simple a rule as possible, and saying that all members, variables and so on are visible within the surrounding curly brackets is a very simple rule. 为什么它被定义为只有Java的创建者能够以任何程度的确定性回答的东西,但我怀疑他们想要尽可能简单的规则,并且说所有成员,变量等在周围卷曲中是可见的括号是一个非常简单的规则。
And by the way: You are not required to import the type, you can use a qualified name instead. 顺便说一下:您不需要导入类型,您可以使用限定名称。 In your example, that would read:
在您的示例中,这将是:
public class Example implements Iterable<Example.Nested> { }
Nested classes are referenced as <OuterClass>.<InnerClass>
, so Iterable<Example.Nested>
would be the typical way to do this in Java, though using import
is also an option. 嵌套类被引用为
<OuterClass>.<InnerClass>
,因此Iterable<Example.Nested>
将是在Java中执行此操作的典型方法,尽管使用import
也是一种选择。
See Nested Classes in the Java Tutorial. 请参阅Java教程中的嵌套类 。
The following code is not yet "inside" the class body 以下代码尚未在类体内部“内部”
public class Example implements Iterable<Nested>
{
// class body starts here
Nested is refered to outside the class and so you must either import
or use Example.Nested
to have Nested in scope. 嵌套是指在类外部,因此您必须
import
或使用Example.Nested
以在范围内嵌套。
if you put similar code inside the class body it will work: 如果你在类体中放入类似的代码,它将起作用:
{
// class body starts here
public void foo(Iterable<Nested> x) ...
}
This works just fine because you are now within the class scope where nested is defined and available unqualified 这样可以正常工作,因为您现在位于已定义嵌套且可用不合格的类范围内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.