Is this a variation of an anonymous inner class?

大城市里の小女人 提交于 2019-11-26 18:35:41

问题


Here is an example

JPanel panel = new JPanel(){
    @Override
    protected void paintComponent(Graphics g){
        // do stuff
    }

    @Override
    public Dimension getPreferredSize(){
        // do stuff
    }
};

Would this just be a variation of an anonymous inner class, or is it something else entirely?


回答1:


Yes that is an anonymous inner class




回答2:


You may be confused about the anonymity of the class because at first blush it looks like you're defining panel to be an instance of JPanel. However, that's not what you are doing. Instead you are defining a sub-class of JPanel, which is a new class and creating panel to be an instance of this new sub-class. What is the name of this new class? Well, it doesn't have one and hence that's what makes it anonymous!




回答3:


That is an anonymous inner class.



来源:https://stackoverflow.com/questions/6432545/is-this-a-variation-of-an-anonymous-inner-class

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