Java题求解:(不用数组)编写一个学生类student,包含的属性有学号、姓名、年龄,并实现如下

2024-11-17 23:30:35
推荐回答(1个)
回答(1):

import java.util.LinkedList;
import java.util.List;

public class Student {

private int id; // 学号
private String name; // 姓名
private int age; // 年龄
private int sex; // 性别 0,女;1,男

List students = new LinkedList();

/**
* 构造方法
*
* @param id
* @param name
* @param age
* @param sex
*/
public Student(int id, String name, int age, int sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}

/**
* 增加学生
*
* @param s
*/
public void add(Student s) {
if (students != null)
students.add(s);
}

/**
* 根据学号删除学生
*
* @param id
*/
public void deleteById(int id) {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
if (s.getId() == id)
students.remove(s);
}
}

}

/**
* 所有学生年龄增加一岁
*/
public void addAgeAll() {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
students.get(i).setAge(students.get(i).getAge() + 1);
}
}
}

/**
* 按顺序显示所有学生信息
*/
public void printAllStudent() {
if (students != null) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t"
+ s.getSex() + "\t" + s.getAge());
}
}
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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 int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}

}