package com.example.demo03;
public interface Engine {
public void installEngine();
}
package com.example.demo03;
public class Engine2000 implements Engine{
@Override
public void installEngine() {
System.out.println("安装2000CC发动机");
}
}
package com.example.demo03;
public class Engine2200 implements Engine{
@Override
public void installEngine() {
System.out.println("安装2200CC发动机");
}
}
package com.example.demo03;
public class Bus extends Car {
public Bus(Engine engine) {
super(engine);
}
@Override
public void installEngine() {
System.out.print("Bus:");
this.getEngine().installEngine();
}
}
package com.example.demo03;
public abstract class Car {
private Engine engine;
public Car(Engine engine){
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public abstract void installEngine();
}
package com.example.demo03;
public class Jeep extends Car {
public Jeep(Engine engine) {
super(engine);
}
@Override
public void installEngine() {
System.out.print("Jeep:");
this.getEngine().installEngine();
}
}
package com.example.demo03;
import org.junit.Test;
public class MainClass {
@Test
public void test01(){
Engine engine2000 = new Engine2000();
Engine engine2200 = new Engine2200();
Car bus1 = new Bus(engine2000);
bus1.installEngine();
Car bus2 = new Bus(engine2200);
bus2.installEngine();
Car jeep1 = new Jeep(engine2000);
jeep1.installEngine();
Car jeep2 = new Jeep(engine2200);
jeep2.installEngine();
}
}