扩展类时遇到问题(不明白出了什么问题,也不明白这个网站上的其他复杂解释



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

.class意料之中。

这是代码:

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);
    }
}

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

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

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

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

这可能是你打算做的。

这是

错误的

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

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

super(200,50,50,1);

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

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

相关内容

最新更新