[20-05-04][Thinking in Java 4]Java Inheritance 2 - Proxy
Posted mirai3usi9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[20-05-04][Thinking in Java 4]Java Inheritance 2 - Proxy相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
[20-05-04][Thinking in Java 8]Java Polymorphism 2 - Late Binding
Hacker Rank: Two Strings - thinking in C# 15+ ways
Thinking in Java & Writing in Python