演示代碼以下:spa
1 public class Computer { 2 public Computer() { 3 CPU cpu=new CPU(); 4 RAM ram=new RAM(); 5 Disk disk=new Disk(); 6 } 7 } 8 class CPU{ } 9 class RAM{ } 10 class Disk{ }
演示代碼以下:代理
1 public class PlaneDelegation{ 2 private PlaneControl planeControl; //private外部不可訪問 3 /* 4 * 飛行員權限代理類,普通飛行員不能夠開火 5 */ 6 PlaneDelegation(){ 7 planeControl=new PlaneControl(); 8 } 9 public void speed(){ 10 planeControl.speed(); 11 } 12 public void left(){ 13 planeControl.left(); 14 } 15 public void right(){ 16 planeControl.right(); 17 } 18 } 19 20 final class PlaneControl {//final表示不可繼承,控制器都能繼承那還得了。。 21 protected void speed() {} 22 protected void fire() {} 23 protected void left() {} 24 protected void right() {} 25 }