Here is an example in << Think in Java >>, showing the sequence in inheritance relationship (Class): ---------------------- -------------------------------------------------- ------------------- // from 'Thinking in Java, 2nd Ed.' By Bruce Eckel // www.bruceeckel.com. See Copyright NOTICE IN CopyRight.txt. // Order of Constructor Calls.
Class meal {meal () {system.out.println ("meal ()");}}
Class break {bow () {system.out.println ("Bread ()");}}
Class cheese {cheese () {system.out.println ("cheese ()");}}
Class lettuce {lettuce () {system.out.println ("lettuce ()");}}
Class Lunch Extends MEAL {Lunch () {System.out.println ("Lunch ()");}}
Class PortableLunch Extends Lunch {PortableLunch () {System.out.println ("PortableLunch ()");}}
Class Sandwich Extends PortableLunch {BREAD B = New Bread (); Cheese C = New Cheese (); lettuce l = new lettuce (); sandwich () {system.out.println ("Sandwich ()");} public static void Main (String [] args) {new sandwich ();}}
operation result:
-------------------------------------------------- ----------------------------------------- meal () lunch () portablelunch () Bread () cheese () lettuce () sandwich ()
This, it is understood that the sequence of constructor calls is as follows:
1. Repeat the constructor of Base Class to ensure that the root Class is first constructed;
2. Initialize a member according to the order of statements;
3. Call the Derived Class constructor system;