我正在阅读有关接口(interface)和类中的内部类的信息。我不了解真正的用途。但是,我不想在本文中讨论关于接口(interface)内部的内部类的任何事情。

到目前为止,我一直使用内部类作为回调。我可以在某个地方以外的地方声明类。

假设我有一个学生列表,我想按ID对他们进行排序。我必须实现Comparator接口(interface),并将其作为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;
            }
        }

    }

}

我也可以这样做
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;
        }
    }

}

我不认为上面的示例中的内部类没有任何重要的意义,除非将其声明为私有(private)类。当我将其声明为私有(private)时,只有封闭的类可以使用它。这意味着该类与封闭的类紧密地绑定(bind)在一起,我看到了这样做的一些优势。

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

最佳答案

如果需要与您的类(class)有关的特定知识,则应该使用内部类。内部类的一个很好的例子可以在这里找到: java.awt.geom.Ellipse2D 以及对应的 Ellipse2D.Double Ellipse2D.Float ( Ellipse2D source code)。您可以将这些类放在包中,但它们比嵌套类更有意义。它们直接对应于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++;
        }
    }
}

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

10-07 19:15
查看更多