如何在Java类中定义类属性ID
展开全部
类属性:又叫做静态属性,即被static修饰。
public class Student {
private String name;
private String studentNum;
private String gender;
private Integer age;
private static Integer id;
static{
getId();
}
public Student() {
}
public Student(String name, String studentNum, String gender, Integer age) {
this.name = name;
this.studentNum = studentNum;
this.gender = gender;
this.age = age;
}
public static Integer getId(){
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNum() {
return studentNum;
}
public void setStudentNum(String studentNum) {
this.studentNum = studentNum;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", studentNum=" + studentNum + ", gender=" + gender + ", age=" + age + "]";
}
}
public class Student {
private String name;
private String studentNum;
private String gender;
private Integer age;
private static Integer id;
static{
getId();
}
public Student() {
}
public Student(String name, String studentNum, String gender, Integer age) {
this.name = name;
this.studentNum = studentNum;
this.gender = gender;
this.age = age;
}
public static Integer getId(){
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNum() {
return studentNum;
}
public void setStudentNum(String studentNum) {
this.studentNum = studentNum;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", studentNum=" + studentNum + ", gender=" + gender + ", age=" + age + "]";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询