2014-12-07 131 views
0

我試圖編程一個類,遵循下面的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類:

公共類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.."); 

    } 


} 

我嘗試創建在RunnerM1AbramsTank對象:

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

回答

2

試試這個:

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

有沒有一種方法可以設置類,以便在創建車輛對象時不需要指定引擎類型?或者這是根據UML創建對象的正確方法? – 2014-12-07 14:58:51

+1

你可以做的是在PoweredVehicle中創建一個重載的構造函數,即'PoweredVehicle(){super(); this.engine = new SteamEngine(100);}'< - 這基本上意味着如果你沒有指定一個引擎(像你原來的tank1 decleration),那麼它將創建一個默認引擎。然而,問題在於它總是會爲沒有指定發動機的所有'poweredVehicles'創建蒸汽引擎。所以上面的代碼是正確的方法。 – gudthing 2014-12-07 15:04:30

+0

好吧,這是有道理的,我如何創建一個坦克對象的列表? – 2014-12-07 15:51:16

相關問題