Explain the code and provide the output class AA private void f Systemoutprint AAf ; protected void g Systemoutprint AAg ; public void h f; g; Systemoutprint AAh ; class BB extends AA pri
The code defines three classes: AA, BB, and Test.
Class AA has three methods:
- private void f()
- protected void g()
- public void h()
Method f() prints "AA.f " to the console. Method g() prints "AA.g " to the console. Method h() calls f(), g(), and prints "AA.h " to the console.
Class BB extends AA and has two methods:
- private void f()
- public void g()
Method f() prints "BB.f " to the console. Method g() prints "BB.g " to the console.
Class Test has a main method that creates a new instance of BB and calls its h() method.
When the program is run, the output will be: AA.f BB.g AA.h
This is because the h() method of BB first calls the f() method of the superclass AA, which prints "AA.f ". Then it calls the g() method of BB, which prints "BB.g ". Finally, it prints "AA.h " as defined in the h() method of AA.
原文地址: http://www.cveoy.top/t/topic/Z9d 著作权归作者所有。请勿转载和采集!