1. First list the code
Base. java
public class Base{ Base() { preProcess(); } void preProcess() {}}
Derived. java
public class Derived extends Base{ public String whenAmISet = "set when declared"; @Override void preProcess() { whenAmISet = "set in preProcess()"; }}
Main. java
public class Main{ public static void main(String[] args) { Derived d = new Derived(); System.out.println( d.whenAmISet ); }}
2. Running result
3. Cause
See http://blog.csdn.net/jltxgcy/article/details/17398751.
1. Call the parent class to initialize global variables. No global variables are required here.
2. Call the parent class constructor, which calls the member function preProcess of the subclass. whenAmISet is set to "set in preProcess ".
3. Call the subclass global variable initialization. whenAmISet is set to "set when declared ".
4. Call the constructor of the subclass.
4. Fan Wai
If the global variable whenAmISet does not set the initialization value, it is set to the default value. The object is null, And the int value is 0.
Local variables do not set initialization values. They must be displayed and cannot be used directly.
The initialization parameters must be displayed in the C ++ constructor initialization list. Different from Java.