难以扩展课程(不明白什么是错的,不理解这个网站上的其他复杂解释。)

时间:2016-03-12 22:37:31

标签: java

基本上,super(int health, int strength, int speed, int type);行不断给我一个错误,说明......

  

.class expected。

以下是代码:

public class Pokemon {
    private int health;
    private int strength;
    private int speed;
    private int type;

    public Pokemon(int health, int strength, int speed, int type) {
      assert health >= 0;
      assert health <= 300;
      assert strength >= 1;
      assert strength <= 3;
      assert speed >= 1;
      assert speed <= 300;
      assert type >= 1;
      assert type <= 3;

      this.health = health;
      this.strength = strength;
      this.speed = speed;
      this.type = type; 
    }
}

public class PokemonTester extends Pokemon {
    PokemonTester() {
       super(int health, int strength, int speed, int type);
    }
    {
       Pokemon charizard = new Pokemon(100,2,50,1);
       Pokemon blastoise = new Pokemon(150,2,150,1);
       Pokemon venusaur = new Pokemon(300,2,100,1);
    }
}

3 个答案:

答案 0 :(得分:3)

删除参数类型并向类构造函数添加参数。

PokemonTester(int health, int strength, int speed, int type) {
   super(health, strength, speed, type);
}

答案 1 :(得分:3)

调用super类的构造函数时,您必须实际传递值。这可以通过在构造函数中包含超类的参数来完成:

PokemonTester(int health, int strength, int speed, int type) {
   super(health, strength, speed, type);
}

这可能是你打算做的。

答案 2 :(得分:3)

这是错误的

super(int health, int strength, int speed, int type);

U应该在这里传递值而不是“声明它们”,例如

super(200,50,50,1);

编译得很好。 super这里意味着你正在调用超类的构造函数(当前类正在扩展的类) 在您的情况下,这将是对Pokemon

的构造函数的调用

通常,您希望将当前构造函数中的一些参数传递给超类的基础,非默认(带有一些参数)构造函数