首页 > 编程语言 > 详细

[20-05-04][Thinking in Java 5]Java Inheritance 3 - Composition & Inheritance

时间:2020-05-04 14:58:59      阅读:51      评论:0      收藏:0      [点我收藏+]
1 package test_1_3;
2 
3 public class Plate {
4 
5     public Plate(int i) {
6         
7         System.out.println("this is Plate");
8     }
9 }

 

 1 package test_1_3;
 2 
 3 public class DinnerPlate extends Plate{
 4 
 5     public DinnerPlate(int i) {
 6         
 7         super(i);
 8         System.out.println("this is DinnerPlate");
 9     }
10 }

 

1 package test_1_3;
2 
3 public class Utensil {
4 
5     public Utensil(int i) {
6         
7         System.out.println("this is Utensil");
8     }
9 }

 

 1 package test_1_3;
 2 
 3 public class Spoon extends Utensil{
 4 
 5     public Spoon(int i) {
 6 
 7         super(i);
 8         System.out.println("this is Spoon");
 9     }
10 }

 

 1 package test_1_3;
 2 
 3 public class Fork extends Utensil{
 4     
 5     public Fork(int i) {
 6         
 7         super(i);
 8         System.out.println("this is Fork");
 9     }
10 
11 }

 

 1 package test_1_3;
 2 
 3 public class Knife extends Utensil{
 4 
 5     public Knife(int i) {
 6         
 7         super(i);
 8         System.out.println("this is Knife");
 9     }
10 }

 

 1 package test_1_3;
 2 
 3 public class Custom {
 4     
 5     public Custom(int i) {
 6         
 7         System.out.println("this is Custom");
 8     }
 9 
10 }

 

 1 package test_1_3;
 2 
 3 public class Table extends Custom{
 4 
 5     private Spoon spoon;
 6     private Fork fork;
 7     private Knife knife;
 8     private DinnerPlate dinnerPlate;
 9     
10     public Table(int i) {
11         
12         super(i + 1);
13         spoon = new Spoon(i + 2);
14         fork = new Fork(i + 3);
15         knife = new Knife(i + 4);
16         dinnerPlate = new DinnerPlate(i + 5);
17         System.out.println("this is Table");
18     }
19     
20     
21     public static void main(String[] args) {
22 
23         Table table = new Table(1);
24     }
25 
26 }

 

结果如下:

this is Custom
this is Utensil
this is Spoon
this is Utensil
this is Fork
this is Utensil
this is Knife
this is Plate
this is DinnerPlate
this is Table

[20-05-04][Thinking in Java 5]Java Inheritance 3 - Composition & Inheritance

原文:https://www.cnblogs.com/mirai3usi9/p/12826251.html

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