java写一个学生类包含属性有学号性别姓名年龄 实现操作增加一个学生 根据学号删除某个学生
时间: 2023-05-30 14:02:06 浏览: 128
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;
}
// getter和setter方法
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) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
break;
}
}
}
}
阅读全文