编写一个标准的JavaBean,名叫Teacher,包含属性id、name、sex、education、school。
时间: 2024-10-09 22:10:33 浏览: 49
在Java中,一个标准的JavaBean通常是一个POJO(Plain Old Java Object),它代表了一个简单的数据对象,用于数据传输和持久化。以下是`Teacher`类的一个简单实现:
```java
import java.io.Serializable;
public class Teacher implements Serializable {
private static final long serialVersionUID = 1L; // 序列化版本ID
// 属性
private int id;
private String name;
private String sex;
private String education;
private String school;
// 构造函数,用于初始化
public Teacher(int id, String name, String sex, String education, String school) {
this.id = id;
this.name = name;
this.sex = sex;
this.education = education;
this.school = school;
}
// 获取属性的方法
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
// 如果需要,可以添加equals(), hashCode(), toString()等方法
}
```
阅读全文