У меня есть класс с именем Robot.java:Наследование конструкторов из суперкласса?
class Robot {
String name;
int numLegs;
float powerLevel;
Robot(String productName) {
name = productName;
numLegs = 2;
powerLevel = 2.0f;
}
void talk(String phrase) {
if (powerLevel >= 1.0f) {
System.out.println(name + " says " + phrase);
powerLevel -= 1.0f;
}
else {
System.out.println(name + " is too weak to talk.");
}
}
void charge(float amount) {
System.out.println(name + " charges.");
powerLevel += amount;
}
}
и подкласс под названием TranslationRobot.java:
public class TranslationRobot extends Robot {
// class has everything that Robot has implicitly
String substitute; // and more features
TranslationRobot(String substitute) {
this.substitute = substitute;
}
void translate(String phrase) {
this.talk(phrase.replaceAll("a", substitute));
}
@Override
void charge(float amount) { //overriding
System.out.println(name + " charges double.");
powerLevel = powerLevel + 2 * amount;
}
}
Когда я компилирую TranslationRobot.java, я получаю следующее сообщение об ошибке:
TranslationRobot.java:5: error: constructor Robot in class Robot cannot be applied to given types;
TranslationRobot(String substitute) {
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Я понимаю, что это имеет в виду что-то о наследовании от суперкласса, но я действительно не понимаю, в чем проблема.
Конструкторы не наследуются. – Kayaman