1 package test_1_1; 2 3 public class Root { 4 5 /** 6 * 继承时的带参构造器 7 * 8 */ 9 10 public Root(int i) { 11 12 System.out.println("this is Root"); 13 } 14 15 public Component1 component1 = new Component1(1); 16 public Component2 component2 = new Component2(2); 17 public Component3 component3 = new Component3(3); 18 }
1 package test_1_1; 2 3 public class Component1 { 4 5 public Component1(int i) { 6 7 System.out.println("this is component1"); 8 } 9 10 }
1 package test_1_1; 2 3 public class Component2 { 4 5 public Component2(int i) { 6 7 System.out.println("this is component2"); 8 } 9 }
1 package test_1_1; 2 3 public class Component3 { 4 5 public Component3(int i) { 6 7 System.out.println("this is component3"); 8 } 9 }
1 package test_1_1; 2 3 public class Stem extends Root { 4 5 public Stem(int i) { 6 7 super(i); 8 System.out.println("this is Stem"); 9 } 10 11 public static void main(String[] args) { 12 13 Stem stem = new Stem(1); 14 } 15 }
结果如下:
this is component1
this is component2
this is component3
this is Root
this is Stem
[20-05-04][Thinking in Java 1]Java Inheritance 1 - Inheritance Syntax 3
原文:https://www.cnblogs.com/mirai3usi9/p/12825881.html