Documentation

The Java™ Tutorials
Hide TOC
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions何时使用嵌套类、本地类、匿名类和Lambda表达式
Trail: Learning the Java Language
Lesson: Classes and Objects
Section: Nested Classes

When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions何时使用嵌套类、本地类、匿名类和Lambda表达式

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表达式也具有这些优点;但是,它们旨在用于更具体的情况:


Previous page: Method References
Next page: Questions and Exercises: Nested Classes