abstract class and anonymous class [duplicate]

别说谁变了你拦得住时间么 提交于 2019-11-28 14:00:16

It does not create an instance of abstract Two. It creates a concrete, anonymous class that extends Two and instantiates it.

It's almost equivalent to using a named inner class like this:

class One {
    public Two two(String s) {
        return new MyTwo();
    }

    class MyTwo extends Two {
        public int display() {
            System.out.println("display()");
            return 1;
        }
    }
}

Because it implements the missing function display(). It returns an anonymous subclass of Two. You can see this if you look at the compiled files. You will have a One$1.class there, which extends Two.class!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!