java 为什么要使用嵌套类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14437783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why should I use nested classes?
提问by Dane Balia
When is it feasible to nest classes? The most common advantage of it that I see is "shared scope" (use of variables across classes).
什么时候可以嵌套类?我看到的它最常见的优点是“共享范围”(跨类使用变量)。
Is this less attractive/less a best practice than just putting the nested class in it's own file, and passing the arguments through the Constructor?
与将嵌套类放在自己的文件中并通过构造函数传递参数相比,这是否不那么有吸引力/不是最佳实践?
回答by dd619
There are several reasons for using nested classes, among them:
使用嵌套类有几个原因,其中包括:
It is a way of logically groupingclasses that are only used in one place.
It increases encapsulation.
Nested classes can lead to more readable and maintainable code.
Child to parent class connection is simpler as it visually illustratesthe variables and methods of each class.
它是一种对仅在一个地方使用的类进行逻辑分组的方法。
它增加了封装性。
嵌套类可以产生更具可读性和可维护性的代码。
子类到父类的连接更简单,因为它直观地说明了每个类的变量和方法。
回答by Dheeru Mundluru
In addition to those mentioned already, one other benefit is:
除了已经提到的那些之外,另一个好处是:
- Nested classes also help you achieve multiple implementation inheritance(ref: Thinking in Java, page 369 - section "Why inner classes"?). As far I know, there is no other way to achieve it in Java.
- 嵌套类还可以帮助您实现多重实现继承(参考:在 Java 中思考,第 369 页 - 部分“为什么使用内部类?”)。据我所知,在 Java 中没有其他方法可以实现它。
回答by Vinay Sharma
According to me the one case i know when nested classes used, When we see one object(OBJ1) is tightly bind with second object(OBJ2) and we can not create first object (OBJ1) without second object(OBJ2). for an example we have employee object and one associated object is salary and we should not able to create salary object independently. because without employee to whom we are going to give salary.
Provide your feedback if i am wrong.
根据我的说法,我知道使用嵌套类时的一种情况,当我们看到一个对象(OBJ1)与第二个对象(OBJ2)紧密绑定并且没有第二个对象(OBJ2)时我们无法创建第一个对象(OBJ1)。例如,我们有员工对象,一个关联对象是薪水,我们不应该能够独立创建薪水对象。因为没有我们要给薪水的员工。
如果我错了,请提供您的反馈。
Second case when we are using map or map then we can use nested classes to remove map of map to make code easy to understandable.
第二种情况,当我们使用 map 或 map 时,我们可以使用嵌套类来删除 map 的 map 以使代码易于理解。
third when we want to send data to client side and we can send it in single object having all data :)
第三,当我们想将数据发送到客户端时,我们可以将它发送到包含所有数据的单个对象中:)
when we need something which can define component of outer class or we want to define adapter.
当我们需要一些可以定义外部类组件的东西或者我们想要定义适配器时。
回答by alexleonenko
I find private static classes useful when I need to pass a group of related fields into a method and manipulate the same group of data throughout a few method invocations inside a class. Similar to LinkedList.Node class which is not exposed to outside rather used to group links as a single unit.
当我需要将一组相关字段传递给一个方法并在类内的几个方法调用中操作同一组数据时,我发现私有静态类很有用。类似于 LinkedList.Node 类,它不对外公开,而是用于将链接分组为一个单元。