It mainly tests the Starting sequence of static members, static code blocks, and constructors in the parent class and subclass. Compile a base class: car, and a subclass, audicar. Add some debugging information to the method. The Code is as follows.
1. The car class code is as follows:
Package com. Test. classloader. Order;
/**
* Test base class
*
* @ Author Rey
*
*/
Public class car {
Static {
System. Out. println ("[Car]: static method block! ");
}
/**
* Constructor
*/
Public Car (){
Super ();
System. Out. println ("[Car]: constructor! ");
}
Protected string m_scarname = NULL;
/**
* Auto start
*/
Public void start (){
System. Out. println ("[Car]: Start method! ");
}
/**
* Stop
*/
Public void stop (){
System. Out. println ("[Car]: Stop method! ");
}
Public static void dostart (){
Static Method in system. Out. println ("[Car]: dostart method! ");
}
}
2. the Audi class code is as follows:
/**
*
*/
Package com. Test. classloader. Order;
/**
* @ Author Rey
*
*/
Public class audicar extends car {
Static {
System. Out. println ("[audicar]: static method block! ");
}
/**
* Constructor
*/
Public audicar (){
Super ();
System. Out. println ("[audicar]: constructor! ");
}
/**
* Auto start
*/
Public void start (){
System. Out. println ("[audicar]: Start method! ");
}
Public static void dostop (){
System. Out. println ("[audicar] Static Method: dostop method! ");
}
}
3. Test the startup sequence of the car and audicar classes. The testcarmain code is as follows:
/**
*
*/
Package com. Test. classloader. Order;
/**
* @ Author Rey
*
*/
Public class testcarmain {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Audicar aaudicar = new audicar ();
Aaudicar. Start ();
}
}
Print result:
In [Car]: static method block!
In [audicar]: static method block!
[Car]: constructor!
[Audicar]: constructor!
In [audicar]: Start method!
Conclusion:
1. Execute static members and method blocks in the parent class
2. Execute a large static member and method block in the subclass.
3. Execute the parent class Constructor
4. Execute the subclass Construction Method
5. Execute other members and methods in the parent class
6. Execute other members and methods in the subclass. If the new object is a subclass object, directly execute the subclass method without executing the method with the same name and parameter in the parent class.