简体   繁体   English

为什么我必须在定义的类中导入嵌套类?

[英]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 type C (§8.1.6) is the entire body of C , 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.

相关问题 我如何更改我在不同类中定义的变量 - How do i change a variable that i have defined in a different class 在Java中,要使用“super”关键字,我是否必须导入目标类? - In Java, to use the “super” keyword, do I have to import the target class? 我是否必须导入同一程序包中但位于不同文件夹中的类? - Do I have to import a class that is in the same package but in different folders? 为什么我不必导入我刚刚在我的主要 class 中使用它的 class? (爪哇) - Why don't I have to import a class I just made to use it in my main class? (Java) 为什么我不需要导入android“ R”类? - Why I do not need to import the android “R” class? 对于Generic外部类,为什么我需要声明嵌套类是静态的? - For a Generic outerclass, why do i need to declare the nested class static? 为什么可以导入与嵌套类同名的类? - Why can you import a class with the same name as a nested class? 为什么bin文件夹中有这么多CLASS文件? - Why do I have so many CLASS files in bin folder? 在树数据结构中编写程序,我必须写 class 名称和用户定义的数据类型名称相同,为什么? - Writing a program in Tree data Structure, I have to write class name and user defined datatype Name is same why? 静态嵌套类是如何静态的,如果是,那么为什么我们必须使用new对其进行初始化? - How is a static nested class static and if it is then why do we have to use new to initialize it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM