java编写一个学生类,包括学号、姓名、性别、年龄和记录学生总数的数据成员
时间: 2023-12-22 14:17:31 浏览: 130
public class Student {
private int id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private static int total; // 记录学生总数的数据成员
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
total++; // 每创建一个学生对象,总数加一
}
// getter和setter方法
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static int getTotal() {
return total;
}
}
阅读全文