抽象(abstract)升级版变接口(interface) 继承(extends)升级版叫实现(implements) 升级版啊升级版 接口可以多继承

Client

---------------------------------------------------


public class Client{
public static void main(String[] args){
///1 生产引擎;
BMW b = new BMW();
//
Car c = new Car(b);


c.testEngine();

c.e = new Lamborghini();
c.testEngine();

}
}

------

car

-------------------------------

public class Car {
Engine e;

Car(Engine e ){
this.e = e ;
}
public void testEngine(){
e.start();
int i = e.gasValue();
System.out.println( "每千公里耗油量: "+ i );
}
}

---

engine   //接口

-------------

public interface Engine {
void start();
int gasValue();
}

----

BMW

------------

public class BMW implements Engine {
public void start(){
System.out.println( "BMW启动了" );
}
public int gasValue(){
return 100;
}
}

-----

Lamborghini

---------------------

public class Lamborghini implements Engine{
public void start(){
System.out.println( "兰博基尼启动了" );
}
public int gasValue(){
return 200;
}
}

--------------------------内存分析图----------------------

 

posted @ 2015-03-25 23:22  Seven_OverLoad  阅读(199)  评论(0编辑  收藏  举报