Recently in the Java programming thought, most of the content is not meaning, but suddenly see a basic concept (seem to forget), so wrote a test, I think this life will not forget the concept.
The topics are as follows:
Public classSuberextendsLast { PublicSuber () {} PublicSuber (inti) {System.out.println ("Suber (" + i + ")"); } PublicSuber (String i) {Super. Commonbase (i + "-SU")); System.out.println ("Suber (" + i + ")"); } Public Static voidMain (string[] args) {suber suber=NewSuber (); System.out.println ("==============main================"); Suber S2=NewSuber (1); System.out.println ("==============main============="); Suber S3=NewSuber ("3"); System.out.println ("==============main============="); }}classBase1 { PublicBase1 () {System.out.println ("Base1"); }}classCommonBase1extendsBase1 { PublicCommonBase1 () {System.out.println ("CommonBase1"); } Public voidcommonbase (String i) {System.out.println ("CommonBase1 (" + i + ")"); }}classLastextendsCommonBase1 { PublicLast () {System.out.println ("Last"); } PublicLast (inti) {System.out.println ("Last (" + i + ")"); }}
When I put this problem to my yard friends to see, their first reaction is: Who TM write code, readability is too poor, requires refactoring.
But when it comes to specific output, they're still a little skeptical.
Yes, we are now a high-level route, for the basics of things, it is not necessarily grasp.
The results are as follows:
base1commonbase1last==============main================base1commonbase1lastsuber (1 )==============main=============base1commonbase1lastcommonbase1 (3-su) Suber (3)==============main=============
View Code
Result explanation: In one sentence, when the subclass is initialized, the default constructor method of the parent class is called by default, because the parent class is checked for correct syntax!!!
Well, the basic thing, remember, maybe the interview was met!
What do you think the output of this question is? --Java Basic concepts