简体   繁体   中英

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

Just a theoretical question about inheritance.

So let's say I've got a class "GamePanel" which extends JPanel.

I know within the class, if I call super.paint(g) , it will call the parent class (JPanel) paint method.

But if I create an object GamePanel, shouldn't it already inherit all of JPanels methods? So in that case, why doesn't this.paint(g) work? The current object should be able to access that method right?

If not, why does this.setBackgroundColor(...) work?

Because apparently this.setBackgroundColor(...) works as well as super.setBackGroundColor(...) . So it's almost like "super" can access all the methods while "this" can't?

It matters if you've redefined paint in the child class...you need a way within your paint method to call the ancestor paint method, otherwise it could only "call itself" like recursively from within that method...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM