我有这个类(class),Person:
public class Person{
String firstname;
String lastname;
public Person(String fname, String lname){
}
public String toString(){
}
}
这个子类,学生:
public class Student extends Person{
Student(){
super();
}
int studentID;
int level;
public Student(String fName, String lName, int gLevel){
}
public int getLevel(){
}
public String toString(){
}
}
当我在 DrJava 中编译时,出现错误:
cannot find symbol
symbol : constructor Person()
location: class Person
我不确定问题出在哪里。据我了解,使用 super() 构造函数应该可以解决这个问题,并且在代码中甚至没有必要。我的代码与我在网上看到的任何演示 Java 继承的示例相匹配,但我仍然收到此错误。
最佳答案
调用 super();
假设 Person
类中有一个默认构造函数,但实际上并不存在,因为您有一个非默认构造函数。
您必须或者显式提供默认构造函数:
public Person() { }
或在super
语句中传递两个参数:
super("Firstname", "Lastname");
关于Java找不到父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698795/