用Java编写一个学生类,包括学号、姓名、性别、年龄和记录学生总数的数据成员
时间: 2023-12-22 18:04:57 浏览: 110
public class Student {
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private static int total = 0; // 记录学生总数
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
total++;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public static int getTotal() {
return total;
}
}
相关问题
java编写一个学生类,包括学号、姓名、性别、年龄和记录学生总数的数据成员
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;
}
}
用Java编写一个学生类,包括学号、姓名、性别、年龄和记录学生总数的数据
public class Student {
private String studentId;
private String name;
private String gender;
private int age;
private static int totalStudentCount = 0;
public Student(String studentId, String name, String gender, int age) {
this.studentId = studentId;
this.name = name;
this.gender = gender;
this.age = age;
totalStudentCount++;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
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 getTotalStudentCount() {
return totalStudentCount;
}
}
阅读全文