java写一个学生类包含属性有学号性别姓名年龄 实现操作增加一个学生 根据学号删除某个学生 所有学生年龄增加一岁 按数组显示所有学生信息
时间: 2023-05-30 17:02:09 浏览: 151
public class Student {
private String id;
private String gender;
private String name;
private int age;
public Student(String id, String gender, String name, int age) {
this.id = id;
this.gender = gender;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 增加一个学生
public static void addStudent(List<Student> students, Student student) {
students.add(student);
}
// 根据学号删除某个学生
public static void deleteStudent(List<Student> students, String id) {
Iterator<Student> iterator = students.iterator();
while(iterator.hasNext()) {
Student student = iterator.next();
if(student.getId().equals(id)) {
iterator.remove();
}
}
}
// 所有学生年龄增加一岁
public static void increaseAge(List<Student> students) {
for(Student student : students) {
student.setAge(student.getAge() + 1);
}
}
// 按数组显示所有学生信息
public static void displayStudents(List<Student> students) {
for(Student student : students) {
System.out.println("学号:" + student.getId() + ",性别:" + student.getGender() + ",姓名:" + student.getName() + ",年龄:" + student.getAge());
}
}
}
阅读全文