内在阶级的目的是什么?

Kri*_*nya 8 java inner-classes

我正在阅读接口和类中的内部类.我无法理解真正的用途.但是我不想在这篇文章中讨论关于接口内部内部类的任何内容.

到目前为止,我已经使用内部类作为回调.我可以在同一个地方宣布课程.

假设我有一个学生列表,我想按ID排序.我必须实现Comparator接口并将其作为Collections的sort方法的参数提供.

public class StudentList {

    public static void main(String[] args) {

        List<Student> students = new ArrayList<Student>();

        Student student = new Student();
        student.setId(1);
        student.setName("Krishna");
        students.add(student);

        student = new Student();
        student.setId(2);
        student.setName("Chaitanya");
        students.add(student);

        Collections.sort(students, new StudentList().new MyComparator());
    }

    public class MyComparator implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {

            if (o1.getId() < o2.getId()) {
                return 1;
            } else if (o1.getId() > o2.getId()) {
                return -1;
            } else {
                return 0;
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

我也可以这样做

public class StudentList {

    public static void main(String[] args) {

        List<Student> students = new ArrayList<Student>();

        Student student = new Student();
        student.setId(1);
        student.setName("Krishna");
        students.add(student);

        student = new Student();
        student.setId(2);
        student.setName("Chaitanya");
        students.add(student);

        Collections.sort(students, new MyComparator());
    }

}

class MyComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {

        if (o1.getId() < o2.getId()) {
            return 1;
        } else if (o1.getId() > o2.getId()) {
            return -1;
        } else {
            return 0;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我不认为上面例子中的内部类增加了任何重要的重要性,除非它被声明为私有类.当我将其声明为私有时,只有封闭类可以使用它.这意味着该类与封闭类强烈绑定,我看到了这样的优点.

任何人都可以向我解释在应用程序中使用/编写内部类的真正重要性/重要性.

Jus*_*tin 5

如果您需要特定于您所使用的类的东西,您应该使用内部类。内部类的一个很好的例子可以在这里找到:java.awt.geom.Ellipse2D以及相应的Ellipse2D.Doubleand Ellipse2D.FloatEllipse2D源代码)。您可以将这些类放在包中,但它们作为嵌套类更有意义。它们直接对应于Ellipse2D其他地方并且不会有任何用处;此外,它们嵌套的事实提高了使用它们的代码的可读性。另一方面,如果内部类在更通用时很可能有用,那么通常最好将其概括并创建一个常规类。

另外,内部类可以直接访问外部类中的变量。这意味着可以使用内部类来更改外部类。仍然可以获取它的实例以在任一类之外使用。为了说明我所说的:

public class Example {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Foo.Bar bar = foo.getBar(); //note, cannot call new Foo.Bar(); as Bar is dependent on Foo
        for (int i = 0; i < 50; i++){
            System.out.println(bar.get());
        }
    }
}
class Foo {
    int val;
    Bar b;

    public Foo(){
        b = new Bar();
    }
    public Bar getBar(){
        return b;
    }
    public class Bar{
        public Bar(){
            val++;
        }
        public int get(){
            return val++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

内部类的另一个可能用途是为真正想要的内部类创建类似包装类的东西,这对于递归类特别有用。这用于实现LinkedList. 有一次,我实现了这样一个列表,却没有意识到以前已经做过这样的事情。这个想法是,你有你的LinkedList类,以及Node其中的一个类(其中每个节点仅指向下一个/上一个节点并保存一个值)。这样做可以简化代码。Node但是,该类位于 的外部是没有任何意义的LinkedList,因为它会是什么类型的“节点”?因此它应该是一个内部类。