这是匿名内部类的变体吗?
这是一个例子
JPanel panel = new JPanel(){
@Override
protected void paintComponent(Graphics g){
// do stuff
}
@Override
public Dimension getPreferredSize(){
// do stuff
}
};
这只是匿名内部类的变体,还是完全是别的东西?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是一个匿名内部类
Yes that is an anonymous inner class
您可能对该类的匿名性感到困惑,因为乍一看,您似乎正在将 panel 定义为 JPanel 的实例。然而,这不是你正在做的。相反,您定义 JPanel 的子类,这是一个新类,并创建面板作为该新子类的实例。这个新班级的名称是什么?好吧,它没有一个,因此这就是它匿名的原因!
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.