演示代码如下:
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 }
新闻热点
疑难解答