实例化抽象类时会发生什么?什么是匿名内部类? [英] What is happening while instantiating an abstract class? What is an anonymous inner class?

查看:130
本文介绍了实例化抽象类时会发生什么?什么是匿名内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实例化类Person时发生了什么?什么是匿名内部类?

What is happening while instantiating class Person? What is an anonymous inner class?

abstract class Person {
    abstract void eat();
}

class TestAnonymousInner {
    public static void main(String args[]) {
        Person p = new Person() {
            void eat() {
                System.out.println("nice fruits");
            } // what happens here?
        };
        p.eat();
    }
}  

推荐答案

匿名类实际上只是语法糖.

Anonymous classes are really just syntactic sugar.

由于Person是抽象的,因此不能直接创建Person的实例.您必须创建一个继承Person的类的实例.这就是抽象类的设计方式.

Since Person is abstract, you can't directly create an instance of Person. You must create an instance of a class that inherits Person. That's how abstract classes are designed.

但是,当您想使用Person的子类时,有时会很不方便.您必须编写如下内容:

However, sometimes it's quite inconvenient to create a subclass of Person when you want to use this class. You have to write something like this:

class MyClass {
    public static void main(String args[]) {
        Person p = new PersonSubclass();
        p.eat();
    }
}  

class PersonSubclass extends Person {
    void eat() {
       System.out.println("nice fruits");
    }
}

阅读代码的人必须找到PersonSubclass的声明,才能知道代码的作用.

People reading your code has to find the declaration of PersonSubclass in order to know what the code does.

但是让我们考虑问题的根源:之所以不能创建Person的实例,是因为eat没有方法主体.因此,如果为eat创建方法主体,则可以创建" Person的实例.这就是匿名类.它们使您仅通过编写方法主体即可创建抽象类/接口的实例.这就是为什么上面的代码可以这样写的原因:

But let's think about the root of the problem: the reason why you can't create an instance of Person is because eat does not have a method body. So if you create a method body for eat, you can "kind of" create an instance of Person. And this is what anonymous classes are. They allow you to create instances of abstract classes/interfaces just by writing method bodies. That's why the above code can just be written like this:

public static void main(String args[]) {
    Person p = new Person() {
        void eat() {
            System.out.println("nice fruits");
        }
    };
    p.eat();
}

它更短,更容易阅读,不是吗?

It's much shorter and easier to read, isn't it?

那么这里到底发生了什么?

What is really happening here then?

编译器为您创建Person的子类,作为封闭类的内部类.在该子类中,有eat的方法主体.内部类被赋予一个非常特殊的名称(尽管我不记得是什么),以便普通代码无法访问它.

The compiler creates a subclass of Person for you, as an inner class of the enclosing class. And in that subclass, there is your method body for eat. The inner class is given a very special name (I can't remember what though) so that normal code can't access it.

P.S.在Java 8中,引入了lambda.它们还是仅通过一种方法进行接口的语法糖.但是,这不适用于抽象类.

P.S. In Java 8, lambdas are introduced. They are again syntactic sugar for interfaces with only one method. This does not work with abstract classes though.

这篇关于实例化抽象类时会发生什么?什么是匿名内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆