java java中的顶级类是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41714579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:05:17  来源:igfitidea点击:

What is a top level class in java?

java

提问by Harsha Jayamanna

  • What is a top level class in java?
  • What is the definition of a top level class in java ?
  • java中的顶级类是什么?
  • java中顶级类的定义是什么?

I know that this is a basic question, but i could not find a clear and simple answer for this question.

我知道这是一个基本问题,但我找不到这个问题的明确和简单的答案。

回答by yshavit

It's simply any class that's not a nested class. If your file is Foo.java, and your class is Foo, then it's a top level class.

它只是任何不是嵌套类的类。如果您的文件是 Foo.java,而您的类是 Foo,那么它就是顶级类。

// in Foo.java:

public class Foo { // top level class

    public static class NestedBar { // nested class
    }

}

I had always thought this was an informal term, but it turns out it's actually defined in the JLS:

我一直认为这是一个非正式的术语,但事实证明它实际上是在 JLS 中定义的

A top level class is a class that is not a nested class.

A nested class is any class whose declaration occurs within the body of another class or interface.

顶级类是不是嵌套类的类。

嵌套类是其声明出现在另一个类或接口的主体内的任何类。

回答by Erivan

The definition posted is correct, maybe another definition could be: a top-level class is the defined class with the same name of the file '.java' where it's in. however, just as an observation, for the case showed, a nested (not a top-level) 'static' class has the same behavior of a top-level class, in terms of the interaction with the instance members of the class where it has been nested.

发布的定义是正确的,也许另一个定义可能是:顶级类是定义的类,其名称与文件“.java”相同。然而,就像观察一样,对于所显示的案例,嵌套(不是顶级)“静态”类具有与顶级类相同的行为,就与嵌套它的类的实例成员的交互而言。