gpt4 book ai didi

java - 带参数的Java构造函数调用

转载 作者:行者123 更新时间:2023-12-02 11:08:20 25 4
gpt4 key购买 nike

我正在尝试使用以下两个参数在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);

除此之外,我在您发布的代码中看不到声明 StudentIDname成员变量的位置。

您的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com