码迷,mamicode.com
首页 > 编程语言 > 详细

[20-05-04][Thinking in Java 4]Java Inheritance 2 - Proxy

时间:2020-05-04 13:35:04      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:class   rgs   print   pack   ant   urb   col   think   private   

 1 package test_1_2;
 2 
 3 public class SpaceShipControls {
 4 
 5     /**
 6      * 代理
 7      */
 8     
 9     public void up(int velocity) {
10         
11         System.out.println("up" + velocity);
12     }
13     
14     public void down(int velocity) {
15         
16         System.out.println("down" + velocity);
17     }
18     
19     public void turboBoost() {
20         
21         System.out.println("turboBoost");
22     }
23 }

 

 

 1 package test_1_2;
 2 
 3 public class SpaceShip {
 4 
 5     private String name;
 6     private SpaceShipControls controls = new SpaceShipControls();
 7     
 8     public SpaceShip(String name) {
 9         
10         this.name = name;
11     }
12     
13     public void up(int velocity) {
14         
15         controls.up(velocity);
16     }
17     
18     public void down(int velocity) {
19         
20         controls.down(velocity);
21     }
22     
23     public void turboBoost() {
24         
25         controls.turboBoost();
26     }
27     
28     public static void main(String[] args) {
29 
30         SpaceShip ship = new SpaceShip("Phantom");
31         ship.up(100);
32         ship.down(100);
33         ship.turboBoost();
34     }
35 
36 }

 

结果如下:

up100
down100
turboBoost

[20-05-04][Thinking in Java 4]Java Inheritance 2 - Proxy

标签:class   rgs   print   pack   ant   urb   col   think   private   

原文地址:https://www.cnblogs.com/mirai3usi9/p/12826113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!