首页 > 编程语言 > 详细

[20-05-04][Thinking in Java 1]Java Inheritance 1 - Inheritance Syntax 3

时间:2020-05-04 11:58:40      阅读:37      评论:0      收藏:0      [点我收藏+]
 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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!