私はゲームと呼ばれる1つのスーパークラスを持っています。それは次のようになります。スーパークラス(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を使い慣れていません。ここで間違っているのは分かりません。ゲームのタワーにデフォルトのコンストラクタが必要なのはなぜですか?
'public game(int corridorLength)'がパラメータ化された –
の重複を持つ場合は、明示的なデフォルトのコンストラクタが必要です:https://stackoverflow.com/questions/1197634/java-error-implicit-super-constructor -is-undefined-for-default-constructor –
Towerコンストラクタは、存在しないGameデフォルトコンストラクタを暗黙的に呼び出します。しかし、Towerは本当にGameを拡張する必要がありますか?私は必要がないと思う。それはそれ自身で立つことができないのですか? – DodgyCodeException