为什么是 super.paint(g) 而不是 this.paint(g)?

Why is it super.paint(g) and not this.paint(g)?

只是一个关于继承的理论问题。

假设我有一个扩展 JPanel 的 class“GamePanel”。

我知道在 class 中,如果我调用 super.paint(g) ,它将调用父级 class (JPanel) 绘制方法。

但是如果我创建一个对象 GamePanel,它不应该已经继承所有的 JPanels 方法吗?那么在那种情况下,为什么 this.paint(g) 不起作用?当前对象应该可以访问那个方法吧?

如果不是,为什么 this.setBackgroundColor(...) 有效?

因为显然 this.setBackgroundColor(...)super.setBackGroundColor(...) 一样有效。所以这几乎就像“super”可以访问所有方法而“this”不能?

如果您在子 class 中重新定义了 paint,这很重要...您需要在 paint 方法中调用祖先 paint 方法,否则它只能从该方法中递归地“调用自身”...