有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    Here是嵌套类的完整文档

    如果您想知道何时使用内部类(从link):

    Compelling reasons for using nested classes include the following:

    It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

    It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

    It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

    是的,您可以从其他类访问内部类。(阅读controlling access to members of a class

    Here您可以找到一个关于如何从类的“外部”访问内部类的示例