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;
}
}
--------------------------内存分析图----------------------