Java 构造函数错误
我被告知,当创建一个新对象时,它需要有与其构造函数相同的参数。我试过了,但还是有这些错误。找不到符号s1.getCourse s1.getName s1.getAge。还有一个无效的构造函数错误。这是我的密码Java 构造函数错误,java,Java,我被告知,当创建一个新对象时,它需要有与其构造函数相同的参数。我试过了,但还是有这些错误。找不到符号s1.getCourse s1.getName s1.getAge。还有一个无效的构造函数错误。这是我的密码 public class Person{ private String name; private int age; public Person(String name, int age){ this.name=name; this.ag
public class Person{
private String name;
private int age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
public String getDetails(){
return "Name: " + name + "Age: " + age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public class Student extends Person{
private String course;
public Student(String name, int age,String course){
super(name,age);
this.course = course;
}
public String getDetails(){
return super.getDetails()+"Course: "+ course;
}
public void setCourse(String course){
this.course=course;
}
public String getCourse(){
return course;
}
public String getName(){
return name;
}
}
public String getAge(){
return age;
}
}
测试人员
student++;
String name=array[0];
int age=Integer.parseInt(array[1]);
String course=array[3];
Student s1= new Student(name,age,course);
System.out.println("name "+s1.getName());
System.out.println("age "+ s1.getAge());
System.out.println("course: " + s1.getCourse());
}
}
有关更多详细信息,请提供Person类 您尚未在学生类中提供name和age属性。
如果所有这些都存在于super类中,那么当您调用s1.getName()方法时,它也将从student类中调用。因为它出现在子类中,即学生请向我们显示错误消息。另外,您是否正在导入
学生
类?Anre名称
和年龄
受保护或公共?我不明白当年龄
的数据类型不匹配时,这将如何编译。在一个地方你指定它为int
,在另一个地方你指定它为String
。为什么getAge()
不在学生课堂上?TestPerson
中的两个大括号是什么意思。还有一个地方age
将int
作为参数,在getAge
方法中,您将其作为String
返回,甚至不将其转换为String。请出示正确的密码。这很让人困惑,你不需要定义getName和getAge,它们应该被继承。getDetails应该在字符串的开头有另一个空格,否则它可能不可读。有人能帮忙吗?它说找不到符号s1.getName(),但它们是通过person类继承的。