super.paint(g)的用途是什么?

unk*_*ror 9 java applet awt paint super

有人可以解释我在super.paint(g)哪里使用,gGraphics Applets或awt或swing或Java中的变量.

我做过研究,发现它用于覆盖但是这个覆盖的用途是什么?

我是初学者.如果可能的话,你可以解释之间的差异paint(g),并super.paint(g)用小example或请帮助我的代码?

/*
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);
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

  1. 首先,我在自己的 Paint 方法中添加 super.paint(g) 。

重新绘制时,之前绘制的内容将被清除。

抱歉,我无法发布超过 2 个链接。

  1. 然后,我在自己的绘制方法中删除了 super.paint(g) 。

    删除 super.paint(g)

可以看到之前在Panel上画的东西还在那里。

当您添加 super.paint(g) 时,在您的方法中它将调用该子类的超类中的方法。我的类 RussiaPanel 扩展了 JPanel,而 JPanel 扩展了 JComponent。它会调用JComponet中的“public void Paint(Graphic g)”方法,Panel上的东西会被清除。更多详细信息,您可以参考API文档。

希望对你有帮助,并原谅我糟糕的英语。