It's not easy to get it straight when it starts to touch.
to create a subclass or polymorphic condition:/* When creating subclasses: 1. Parent class static code block 2. Subclass static code block 3. The parent class constructs a block/instance variable (a member of a variable created by new)//WHO executes before executing the constructor 4. Parent class Constructors//parent class constructors have methods
, the parent class method is not executed if the method subclass has a method that executes the subclass.
//If the subclass has a parent class that does not have an error!//parent class Private The method executes the parent class method5. Subclass constructs block/instance variable//WHO executes the constructor 6 after execution. Subclass Constructor 7. Subclass Normal Method//Call member variable, look at the value of the subclass, no longer look at the value of the parent Class 8. The child class member variable//does not call the value of the parent Class 9. Normal method Output result
Class a{ int m=1; {System.out.println ("111");} 1-->111 A () { speak ();//4--> directly executes the subclass method 5 } c c = new C (); 2--> {System.out.println ("222");} void Speak () { System.out.println ("a");} } Class c{ C () { System.out.println ("C");//3-->c //7-->c }}class B extends a{ int m = 5; 10--> {System.out.println ("333");} B () { System.out.println ("B");//8-->b } c c = new C (); 6--> void Speak () { System.out.println (m);//5-->0 //9--> //11-->5 } {System.out.println ("444");} public static void Main (string[] args) { b b = new B (); B.speak (); }}
The comments are followed by the execution order and output results.
A code block is constructed in the child parent class in the code static block. Code block constructor member variable instance variable execution order