没有可用的构造函数我有一个超类,它被称为游戏。它看起来像这样:有在超类(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