The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
As mentioned in the section Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code.如嵌套类一节所述,嵌套类使您能够对仅在一个位置使用的类进行逻辑分组,增加封装的使用,并创建更可读和可维护的代码。Local classes, anonymous classes, and lambda expressions also impart these advantages; however, they are intended to be used for more specific situations:本地类、匿名类和lambda表达式也具有这些优点;但是,它们旨在用于更具体的情况:
Local class局部类: Use it if you need to create more than one instance of a class, access its constructor, or introduce a new, named type (because, for example, you need to invoke additional methods later).:如果需要创建一个类的多个实例、访问其构造函数或引入新的命名类型(例如,因为以后需要调用其他方法),请使用它。
Anonymous class匿名类: Use it if you need to declare fields or additional methods.:如果需要声明字段或其他方法,请使用它。
Use it if you are encapsulating a single unit of behavior that you want to pass to other code.如果要封装要传递给其他代码的单个行为单元,请使用它。For example, you would use a lambda expression if you want a certain action performed on each element of a collection, when a process is completed, or when a process encounters an error.例如,如果希望在流程完成或流程遇到错误时对集合的每个元素执行特定操作,则可以使用lambda表达式。
Use it if you need a simple instance of a functional interface and none of the preceding criteria apply (for example, you do not need a constructor, a named type, fields, or additional methods).如果您需要函数接口的简单实例,且上述条件均不适用(例如,您不需要构造函数、命名类型、字段或其他方法),请使用它。
Nested class嵌套类: Use it if your requirements are similar to those of a local class, you want to make the type more widely available, and you don't require access to local variables or method parameters.:如果您的需求与本地类的需求相似,您希望使该类型更广泛地可用,并且不需要访问本地变量或方法参数,请使用它。
Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods.如果需要访问封闭实例的非公共字段和方法,请使用非静态嵌套类(或内部类)。Use a static nested class if you don't require this access.如果不需要此访问,请使用静态嵌套类。