Parent class, subclass initialization [2]

xiaoxiao2021-03-06  47

When loading a class, the parent class is static, then static, then the main method in the subclass, if there is a statement generated by the subclass object in the main method, first find the subclass constructor, because each subclass constructor will have Super statement (JDK default or itself, and default in the first line in the constructor), first initialize the parent class is non-static, then the parent class is constructed, and then the subclass is non-static, the subclass constructor (except SUPER) ), The last is the remaining statement. Overall: The parent class is preceded by the subclass, and the order in the same kind is static, non-static, constructor. Such as:

Public class test3 extends test4 {{system.out.print ("1");}

TEST3 () {//system.out.print ("a "); // Super (3); System.out.Print (" b "); system.out.print (" 2 ");}

Static {system.out.print ("3");

{System.out.print ("4");

Public static void main (String [] args) {system.out.print ("7"); new test3 (); system.out.print ("a");}

Static {system.out.print ("d");}}

Class test4 {{system.out.print ("8");} test4 () {system.out.print ("e");} test4 (int i) {system.out.print ("5");} Static {system.out.print ("6");} {system.out.print ("9");} static {system.out.print ("c");}} The answer is 6C3D789E14B2APress any key to contact. .

转载请注明原文地址:https://www.9cbs.com/read-80823.html

New Post(0)