gpt4 book ai didi

java - 创建一个类和在另一个类中声明一个类有区别吗?

转载 作者:行者123 更新时间:2023-11-30 03:13:30 26 4
gpt4 key购买 nike

我想知道在新文件中创建类和在另一个类中声明类之间是否有任何区别,是否仍然可以从类外部引用它?我很想知道,因为这会帮助我的工作场所更有条理。

最佳答案

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您可以找到有关如何从类的“外部”访问内部类的示例。

关于java - 创建一个类和在另一个类中声明一个类有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33160850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com