组合、继承和代理三者的定义:
演示代码如下:
?public class Computer {
? ? public Computer() {
? ? ? ? CPU cpu=new CPU();
? ? ? ? RAM ram=new RAM();
? ? ? ? Disk disk=new Disk();
? ? }
}
class CPU{? ? }
class RAM{? ? }
class Disk{? ? }
继承:子类需要具有父类的功能,各子类之间有所差异。like Shape类作为基类,子类有Rectangle,CirCle,Triangle……代码不写了,大家都经常用。
演示代码如下:
?
public class PlaneDelegation{? ?
? ? private PlaneControl planeControl;? ? //private外部不可访问
? ? /*
? ? * 飞行员权限代理类,普通飞行员不可以开火
? ? */
? ? PlaneDelegation(){
? ? ? ? planeControl=new PlaneControl();
? ? }
? ? public void speed(){
? ? ? ? planeControl.speed();
? ? }
? ? public void left(){
? ? ? ? planeControl.left();
? ? }
? ? public void right(){
? ? ? ? planeControl.right();
? ? }
}
final class PlaneControl {//final表示不可继承,控制器都能继承那还得了。。
? ? protected void speed() {}
? ? protected void fire() {}
? ? protected void left() {}
? ? protected void right() {}
}?