gpt4 book ai didi

Java 无法将 From Student 转换为 Student[]

转载 作者:行者123 更新时间:2023-12-01 19:12:27 27 4
gpt4 key购买 nike

嘿伙计们,我可以得到一些关于这个java项目的帮助吗我的问题是这一行Student s = new Student(r.getChar(),r.getChar(),r.getNum(),r.getNum());我想让它成为一个数组,但如果我尝试替换 Student s 惠特 Student s[i]= new Student(r.getChar()........) 我收到一条错误消息,告诉我无法从 Student 转换对于 Student[],我需要一个解决方案,使其成为一个数组

不介意 r.getChar()

public class Student extends Person {
private int grade;
public Student(String name, String address, int age, int grade) {
super(name, address, age);
this.grade = grade;
}

public void setGrade(int grade){
this.grade = grade;
}

public int getGrade(){
return grade;
}
}

public class Person{
private String name;
private String address;
private int age ;

public Person(String name,String address,int age){
this.name = name;
this.address = address;
this.age = age;
}

public void setName(String name){
this.name = name;
}

public void setAddress(String address){
this.address = address;
}

public void setAge(int age){
this.age = age;
}

public String getName(){
return name;
}

public String getAddress(){
return address;
}

public int getAge(){
return age;
}
}

最佳答案

以下是创建 Student 数组的方法:

Students allStudents[] = new Students[x];

这将创建一个由 x 元素组成的数组,所有元素都初始化为 null。然后,您将在循环或其他内容中初始化每个元素:

allStudents[i] = new Student(...);

关于Java 无法将 From Student 转换为 Student[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960927/

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