开发者

Is this a variation of an anonymous inner class?

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?


Yes that is an anonymous inner class


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!


That is an anonymous inner class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜