沒有可用的構造函數我有一個超類,它被稱爲遊戲。它看起來像這樣:有在超類(JAVA)
import java.util.ArrayList;
public class Game {
private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
private ArrayList<Tower> towers = new ArrayList<Tower>();
private int corridorLength;
private int currentPosition = 0;
public Game(int corridorLength){
this.corridorLength = corridorLength;
}
public void addTower(int damage,int timeStep){
this.towers.add(new Tower(damage,timeStep)); // Add tower with
current position corrdor length
}
public void addEnemy(int health, int speed){
this.enemies.add(new Enemy(health,speed));
}
public void advance(){
this.currentPosition = this.currentPosition + 1;
if(this.currentPosition == this.corridorLength){
System.out.println("Game Over");
}
}
public void printDamage(){
System.out.println(this.towers.get(this.currentPosition));
}
}
主要焦點是公共無效addTower(INT,INT) 所以,我有一個叫做塔子類:
public class Tower extends Game {
public Tower(int damage, int timeStep){
super.addTower(damage,timeStep);
}
public void getDamage(){
super.printDamage();
}
}
和塔子類的子類,稱爲投石車:
public class Catapult extends Tower {
public Catapult(){
super(5,3);
}
}
我是新來的Java並不能看到我在做什麼錯在這裏。爲什麼我需要在遊戲中使用Tower的默認構造函數?
如果你有一個'public Game(int corridorLength)'參數化一個 –
可能的重複,你需要一個顯式的默認構造函數:https://stackoverflow.com/questions/1197634/java-error-implicit-super-constructor -is-未定義換默認構造函數 –
塔的構造函數隱式調用遊戲默認的構造函數,它不存在。但塔真的需要擴展遊戲嗎?我沒有看到任何需要。難道它不能獨立嗎? – DodgyCodeException