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

标签 java

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

最佳答案

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/

相关文章:

java - Hibernate - 从多个字段到同一实体的关系

java - com.jcraft.jsch.JSchException : verify: false 错误

java - 在 Java 中从哪里获取 'http' 字符串文字?

java - uber jar 内的 HttpServlet 可以发送位于 jar 内的页面吗?

java - 是否可以在 Android 中运行 H2O 模型?

java - servlet 过滤器如何在请求页面上发送错误消息?

java - hibernate :无法使用 hibernate 从 MySQL 中提取数据

java - 如何使用 AChartEngine 绘制双 Y 轴(辅助 Y 轴) - Android

java - 以编程方式配置通过 CXF 启动的 jetty

java - 改变静态 boolean 值