设计学生类Student,内含如下属性:学号(int id)、姓名(String name)、数学成绩(int math)、英语成绩(int english)、计算机成绩(int cs)。 创建若干个学生类的对象,将其加入List集合中。将学生对象按照如下规则输出:
时间: 2024-10-18 09:24:49 浏览: 51
设计一个名为`Student`的Java类,可以按照以下结构完成:
```java
public class Student {
private int id;
private String name;
private int mathScore;
private int englishScore;
private int csScore;
// 构造函数
public Student(int id, String name, int math, int english, int cs) {
this.id = id;
this.name = name;
this.mathScore = math;
this.englishScore = english;
this.csScore = cs;
}
// 获取属性的方法
public int getId() { return id; }
public String getName() { return name; }
public int getMathScore() { return mathScore; }
public int getEnglishScore() { return englishScore; }
public int getCsScore() { return csScore; }
// 自定义输出信息的方法
@Override
public String toString() {
return "学号:" + id + ", 姓名:" + name + ", 数学:" + mathScore + ", 英语:" + englishScore + ", 计算机:" + csScore;
}
}
```
然后,你可以创建多个`Student`对象并添加到`ArrayList`中:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
// 创建学生对象并添加到列表中
students.add(new Student(1001, "张三", 85, 90, 92));
students.add(new Student(1002, "李四", 90, 88, 95));
students.add(new Student(1003, "王五", 95, 92, 98));
// 按照特定规则输出学生信息
for (Student student : students) {
System.out.println(student);
}
}
}
```
当运行这个程序时,它会按照提供的`toString()`方法格式输出每个学生的详细信息。
阅读全文
相关推荐


















