Moveable.java
?
public interface Moveable {
?? public void move();
}
?
?
Tank.java类
?
import java.util.Random;
public class Tank implements Moveable {
?public void move() {
??????? System.out.print("正在移动....");
??????? try {
???Thread.sleep(new Random().nextInt(10000));
??} catch (InterruptedException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?}
?
}
?
Tank2.java
?
public class Tank2 extends Tank{? //继承
?@Override
?public void move() {
??long start=System.currentTimeMillis();
??super.move();
??long end=System.currentTimeMillis();
??System.out.print("time:"+(end-start));
?}
?
}
?
Tank3 .java类
?
public class Tank3 implements Moveable{ //聚合,把tank聚合到tank3中
??? Tank t;
?public void move() {
??long start=System.currentTimeMillis();
??t.move();
??long end=System.currentTimeMillis();
??System.out.print("time:"+(end-start));
??
?}
?public Tank3(Tank t) {
??super();
??this.t = t;
?}
}
?
聚合比继承好,继承只能继承一个,实现可以实现多个