java - 尝试创建新对象时构造函数未定义错误
问题描述
只需查看我的代码,为什么这不起作用?只是尝试使用 Student 方法创建新的学生对象。
public class Student {
public String id, first_name,last_name;
//assuming it was subject grades
public String[] subject_grades;
public void Student(String id, String first_name, String last_name, String[] subject_grades) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.subject_grades = subject_grades;
}
public static boolean readFile(String filename) { File file = new File(filename);
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
List<Student> list = new ArrayList<>();
String[] words = scanner.nextLine().split(",");
new Student(words[0], words[1], words[2],
new String[] {words[3], words[4], words[5], words[6], words[7], words[8]}
));
}
} catch (FileNotFoundException e) {
System.out.println("Failed to read file");
}
return true;
}
}
构造函数未定义。
解决方案
public void Student(String id,String first_name,String last_name,String[] subject_grades) {
这不是构造函数。它是一种void
称为Student
.
将其更改为
public Student(String id,String first_name,String last_name,String[] subject_grades) {
推荐阅读
- flutter - 用 textformfield 填充列表视图
- python - 在 Python 中分配多个列的任何更简单的方法,例如 R data.table :=
- javascript - 如何通过在js中迭代对象来过滤对象
- haskell - 帕斯卡三角形使用 Haskell 函数查找位置 x,y
- c# - 如何防止 Stimulsoft 报告中的数字自动舍入?
- javascript - JS管理4个进度条
- terraform - 重新使用 terraform.tfvars 中的值
- spring-boot - 或 Spring Data MongoDB 中的运算符标准
- suitescript2.0 - Suitescript 2.0 添加第三方库
- python - Python-多线程:分段错误