我正在尝试按照下面的UML编程一个类,以演示层次结构,但是当我尝试创建M1AbramsTank时,构造函数不接受SteamEngine的引擎类型。

要求是我添加了The constructor of ArmouredFightingVehicle should accept as a parameter aninstance of PoweredVehicle,但随后我无法指定引擎的类型。

错误是The constructor M1AbramsTank(SteamEngine) is undefined

原因是因为M1Abrams坦克的构造函数采用了PoweredVechicle类型,但是如何指定对象采用哪种引擎呢?



它实现的SteamEngine类和Engine接口:

public class SteamEngine implements Engine {


    //instance variables
    private int speed;



    public SteamEngine(int speed) {

        this.speed = speed;
    }


    public void setSpeed(int speed) {
        this.speed = speed;
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("Steam engine Started:");


    }

    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("Steam engine Started:");


    }

    @Override
    public void throttle(int power) {
        // TODO Auto-generated method stub
        speed += power;


    }

    @Override
    public int getSpeed() {
        // TODO Auto-generated method stub
        return speed;
    }

}


PoweredVechicle类:

公共类PoweredVehicle {

Engine engine;

public PoweredVehicle(Engine engine) {
    super();
    this.engine = engine;
}

public Engine getEngine() {
    return engine;
}


public void  Drive(){


    System.out.println("Generic Driving");

}

public void setEngine(Engine engine) {
    this.engine = engine;
}

public void stop() {
    engine.stop();
}

public void start() {
    engine.start();
}

public void throttle(int power) {
    engine.throttle(power);
}

public int getSpeed() {
    return engine.getSpeed();
}


}

ArmouredVechicle使用指定的PoweredVechicle实例委托其方法:

public class ArmouredFightingVehicle  {

    PoweredVehicle poweredvechicle;

    public ArmouredFightingVehicle(PoweredVehicle poweredvechicle) {
        super();
        this.poweredvechicle = poweredvechicle;
    }



    public void Drive() {
        poweredvechicle.Drive();
        System.out.println("Armoured vehicle driving..");
    }



    public Engine getEngine() {
        return poweredvechicle.getEngine();
    }

    public void Fight() {
        System.out.println("bang bang!");
    }

    public void setEngine(Engine engine) {
        poweredvechicle.setEngine(engine);
    }

    public void stop() {
        poweredvechicle.stop();
    }

    public void start() {
        poweredvechicle.start();
    }

    public void throttle(int power) {
        poweredvechicle.throttle(power);
    }

    public int getSpeed() {
        return poweredvechicle.getSpeed();
    }

    public String toString() {
        return poweredvechicle.toString();
    }


}


M1AbramsTank类:

public class M1AbramsTank extends ArmouredFightingVehicle{



    public M1AbramsTank(PoweredVehicle poweredvechicle) {
        super(poweredvechicle);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void Fight() {
        // TODO Auto-generated method stub
        System.out.println("M1 fighting..");

    }


}


我尝试在M1AbramsTank类中创建一个Runner对象:

M1AbramsTank tank1 = new M1AbramsTank(new SteamEngine(100));

最佳答案

尝试这个:

M1AbramsTank tank1 = new M1AbramsTank(new PoweredVehicle(new SteamEngine(100)));

08-04 17:28