创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩
时间: 2024-05-05 13:21:07 浏览: 100
以下是一个示例代码,创建一个包含三个学生对象的数组:
```java
class Student {
private int id;
private String name;
private double grade;
public Student(int id, String name, double grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getGrade() {
return grade;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student(1, "Tom", 89.5);
students[1] = new Student(2, "Jerry", 78.0);
students[2] = new Student(3, "Alice", 92.5);
for (int i = 0; i < students.length; i++) {
System.out.println("Student " + students[i].getId() + ": " + students[i].getName() + " - Grade: " + students[i].getGrade());
}
}
}
```
输出结果:
```
Student 1: Tom - Grade: 89.5
Student 2: Jerry - Grade: 78.0
Student 3: Alice - Grade: 92.5
```
阅读全文