我正在尝试使用以下两个参数在Student.java中调用构造函数:
public class InheritanceDemo {
public static void main(String[] args) {
Student s = new Student(String SSname, int SSStudentID);}
s.writeOutput();
}
学生.java
public class Student extends Person{
public Student(String Sname, int SStudentID) {
super(Sname);
StudentID = SStudentID;
}
public void writeOutput() {
System.out.println("Name:" + getName());
System.out.println("StudentNumber:" + StudentID);
}
人.java
public Person() {
name = "No name yet";
}
public Person (String initialName) {
name = initialName;
}
public String getName() {
return name;
}
这里
Person.java
是基类,而Student.java
是子类。正在向我显示以下错误:Multiple markers at this line (near `Student s = new Student(String SSname, int SSStudentID);`
- Syntax error on token "int", delete this
token
- SSStudentID cannot be resolved to a
variable
- String cannot be resolved to a variable
- Syntax error on token "SSname", delete
this token
我该如何解决?
最佳答案
调用方法(或构造函数)时,应传递实际值或变量:
变更:
Student s = new Student(String SSname, int SSStudentID);
到这样的东西:
Student s = new Student("SomeName", 1234);
除此之外,我在您发布的代码中看不到声明
StudentID
和name
成员变量的位置。您的
Student
类应该具有(除了其当前内容之外):public class Student extends Person {
private int StudentID;
}
您的
Person
类应该具有(除了其当前内容之外):public class Person {
private String name;
}
关于java - 带参数的Java构造函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974881/