gpt4 book ai didi

java - 如何创建一个从另一个类调用委托(delegate)方法的对象?

转载 作者:行者123 更新时间:2023-12-02 05:11:03 25 4
gpt4 key购买 nike

我正在尝试按照下面的 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它实现的接口(interface):

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)类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 的实例按照指定委托(delegate)其方法:

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

关于java - 如何创建一个从另一个类调用委托(delegate)方法的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27343837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com