使用参数调用Java构造函数
我正在尝试使用以下两个参数调用Student.java中的构造函数:使用参数调用Java构造函数,java,constructor,compiler-errors,Java,Constructor,Compiler Errors,我正在尝试使用以下两个参数调用Student.java中的构造函数: public class InheritanceDemo { public static void main(String[] args) { Student s = new Student(String SSname, int SSStudentID);} s.writeOutput(); } Student.java public class Student extends Person{ pu
public class InheritanceDemo {
public static void main(String[] args) {
Student s = new Student(String SSname, int SSStudentID);}
s.writeOutput();
}
Student.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);
}
Person.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
成员变量
您的学生
课程应具备(除当前内容外):
您的人员
类应具有(除当前内容外):
主函数中存在语法错误,需要在调用构造函数外部声明变量
SSname和SStudentID
执行以下操作
public static void main(String[] args)
{ Student s = new Student(String SSname, int SSStudentID);}
s.writeOutput();
}
public static void main(String[] args)
{
String SSname = "your_name";
int SSStudentID=10;
Student s = new Student(SSname,SSStudentID );}
s.writeOutput();
}
您的错误将会消失,谢谢。这帮了大忙。
public class Person {
private String name;
}
public static void main(String[] args)
{ Student s = new Student(String SSname, int SSStudentID);}
s.writeOutput();
}
public static void main(String[] args)
{
String SSname = "your_name";
int SSStudentID=10;
Student s = new Student(SSname,SSStudentID );}
s.writeOutput();
}