如何创建一个从另一个类调用委托方法的对象?

时间:2014-12-07 14:49:07

标签: java oop object delegation

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

要求是我添加的The constructor of ArmouredFightingVehicle should accept as a parameter an instance of PoweredVehicle,但我不能指定引擎的类型。

错误为The constructor M1AbramsTank(SteamEngine) is undefined

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

uml hierarchy

它实现的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类:

public class 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));

1 个答案:

答案 0 :(得分:2)

试试这个:

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