基本上,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);
}
}
答案 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
类
通常,您希望将当前构造函数中的一些参数传递给超类的基础,非默认(带有一些参数)构造函数