有人能给我解释一下super.paint(g)
的用法吗?g
是Applets、awt、swings或Java中的Graphics
变量。
我做了一些研究,发现它是用来覆盖的,但是这个覆盖有什么用呢?
我是个初学者。如果可能,你能用一个小的example
来解释paint(g)
和super.paint(g)
之间的区别吗?或者请你帮我写这个代码?
/*
Let us consider this code
This has only one paint declaration i.e; subclass's paint method declaration, no declaration for superclass's paint function... when we explicitly call superclass's paint function
what is the use of super.paint(g) and is it going to use superclass's paint declaration??
*/
import java.awt.*;
import java.applet.*;
/*
<applet code="superpaintDemo" height=768 width=1366>
</applet>
*/
class superpaintDemo extends Applet
{
public void paint(Graphics g)
{
super.paint(g);
g.drawString("This is Demo",200,200);
}
}
发布于 2014-05-03 02:04:30
我猜它的用法就像你使用super.method()
的其他方法一样。这用于调用超类中的方法。基本上,它取决于你的目的,你决定如何使用它。在大多数情况下,我们会覆盖组件子类中的paint(Graphics g)
来实现我们的意图。如果调用super.paint(g)
,它可能会调用此子类的超类中的方法。
如果你想让超类首先(或最后)绘制,你应该调用super.paintComponent(g)
;
在Swing中调用super.paint(g)
会导致讨厌的递归。
https://stackoverflow.com/questions/23434158
复制相似问题