Java 内部类设计的好处

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16084834/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:51:10  来源:igfitidea点击:

Java Inner Classes Design Benefits

javainner-classes

提问by James Oravec

In java, you can have inner classes. I'm curious from a design perspective if there are any benefits to this.

在java中,你可以有内部类。从设计的角度来看,我很好奇这是否有任何好处。

My initial thoughts are that having a separate file with the class is cleaner in separating things into reusable modules. By doing so, if other classes wish to use that other class, they can also make their own instances. To me it seems like avoiding the inner classes may be a better design for extensibility and code reuse, and when working on new projects, its normally hard to tell if the class would be reused or not... so I feel siding on having the separate classes is the way to go.

我最初的想法是,将类分成一个单独的文件可以更清晰地将事物分成可重用的模块。通过这样做,如果其他类希望使用该其他类,它们也可以创建自己的实例。在我看来,避免使用内部类可能是一种更好的可扩展性和代码重用设计,并且在处理新项目时,通常很难判断该类是否会被重用......单独的课程是要走的路。

I ask this question because I took over a project which has a bunch of these... which makes me think the previous developer may of just been lazy or unfamiliar with the IDE. But I want to make sure I'm not missing any benefits that these inner classes have. If there are benefits can someone let me know, and then I can confirm if the previous developer was taking advantage of these benefits.

我问这个问题是因为我接手了一个有很多这样的项目......这让我觉得以前的开发人员可能只是懒惰或不熟悉 IDE。但我想确保我不会错过这些内部类的任何好处。如果有好处可以有人告诉我,然后我可以确认以前的开发人员是否在利用这些好处。

回答by Bohemian

Inner classes are good when they are not used outside the containing class - it's a way of curtailing class bloat. As a general rule, that's the only time I use them.

内部类在不在包含类之外使用时很好 - 这是一种减少类膨胀的方法。作为一般规则,这是我唯一一次使用它们。

Non-static inner classes also have access to the containing instance's private fields.

非静态内部类也可以访问包含实例的私有字段。

回答by Vitaly

Inner classes are useful to:

内部类可用于:

  1. Hide implementation details.

  2. Make project view more concise and thus understandable.

  3. Add logical grouping of the related code.

  1. 隐藏实现细节。

  2. 使项目视图更加简洁易懂。

  3. 添加相关代码的逻辑分组。

回答by dcernahoschi

One good usage of inner classes that comes into my mind is in java.util.ArrayListthat hides its iterators implementations into private inner classes. You can't create them except by invoking iterator()or listIterator()on the list object.

我想到的内部类的一个很好的用法是在java.util.ArrayList中将其迭代器实现隐藏到private inner classes. 除非通过调用iterator()listIterator()在列表对象上,否则无法创建它们。

This way the Iteratorand ListIteratorimplementations for ArrayListare grouped with their related class and methods for enhanced readability (the implementations are pretty short), but hidden from others.

这样Iterator和 的ListIterator实现ArrayList与其相关的类和方法分组以增强可读性(实现非常短),但对其他人隐藏。

They can't be declared staticas they need access to their enclosing instanceobject.

它们不能被声明,static因为它们需要访问它们的封闭实例对象。

public class ArrayList<E> extends AbstractList<E>
         implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    ...
    public Iterator<E> iterator() {
         return new Itr();
    }

    private class Itr implements Iterator<E> {
    ...
    }

    public ListIterator<E> listIterator() {
         return new ListItr(0);
    }

    private class ListItr extends Itr implements ListIterator<E> {
    ...
    }
}

回答by Kanagavelu Sugumar

Non-static inner class:
1) Inner class will be used to perform some task over the outer class and returns the output
. So it will ensure that the outer class object is created to access it.
2) Inner class is only specific to the outer class and no use on the outside world. So no need to think about code reuse.

非静态内部类:
1)内部类将用于在外部类上执行一些任务并返回输出
。所以它将确保创建外部类对象来访问它。
2)内部类只针对外部类,对外没有用。所以无需考虑代码重用。

e.g_1) The best example for inner class is iteratorimplementation of collection framework. Set(AbstractCollection.java) and List(AbstractList.java) will use different iterator inner class implementation.
-> We can reuse the inner class code. For example implementation of private class Itrinnerclass at java.util.AbstractList is used by both Arraylist and Vector.

e.g_1) 内部类最好的例子是iterator集合框架的实现。Set(AbstractCollection.java) 和 List(AbstractList.java) 将使用不同的迭代器内部类实现。
-> 我们可以重用内部类代码。例如,Arraylist 和 Vector 都使用了private class Itr内部类 at 的实现java.util.AbstractList

Static inner class:
1) Static inner class won't depend on a outer class object; but it is specific to outer class level. It wont be exposed to standalone since it is specific to the outer class only. So no code reuse.
2) It has group of methods which is common(shared) to all the objects (Class level) 3)

静态内部类:
1)静态内部类不依赖于外部类对象;但它特定于外部类级别。它不会暴露给独立的,因为它只特定于外部类。所以没有代码重用。
2)它有一组对所有对象(类级别)通用(共享)的方法 3)

e.g_1) Sigleton object retrival using static inner class.
e.g_2) Builder Pattern
e.g_3) Comparator implementation for a class .

e.g_1) 使用静态内部类检索 Sigleton 对象。
e.g_2) Builder Pattern
e.g_3) 类的比较器实现。

回答by Jurass

Inner class are very important thing, it helps you to achieve encapsulation needs very neaty, your base class can also inherit from many classes (implementing abstract handlers for example). Every programmer may learn how to use inner class in Java, it's very useful thing.

内部类是非常重要的东西,它可以帮助您非常整洁地实现封装需求,您的基类还可以从许多类中继承(例如实现抽象处理程序)。每个程序员都可能学习如何在Java中使用内部类,这是非常有用的东西。