JavaBean:标准的Java类
实体类是与数据库表一一对应
-
要求:
1. 类必须被public修饰 2. 必须提供空参的构造器 3. 成员变量必须使用private修饰 4. 提供公共setter和getter方法 -
功能:封装数据
import java.io.Serializable;
//implements Serializable 操作dbutil时需要实现序列化
public class User implements Serializable {
private int uid;
private String uname;
private String upwd;
private String usex;
private String uhobbey;
private String utext;
public User() {
}
public User(int uid, String uname, String upwd, String usex, String uhobbey, String utext) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
this.usex = usex;
this.uhobbey = uhobbey;
this.utext = utext;
}
public User(String uname, String upwd, String usex, String uhobbey, String utext) {
this.uname = uname;
this.upwd = upwd;
this.usex = usex;
this.uhobbey = uhobbey;
this.utext = utext;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
public String getUhobbey() {
return uhobbey;
}
public void setUhobbey(String uhobbey) {
this.uhobbey = uhobbey;
}
public String getUtext() {
return utext;
}
public void setUtext(String utext) {
this.utext = utext;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", upwd='" + upwd + '\'' +
", usex='" + usex + '\'' +
", uhobbey='" + uhobbey + '\'' +
", utext='" + utext + '\'' +
'}';
}
}
本文详细介绍了JavaBean标准类的概念,以及其实现细节。JavaBean作为实体类,用于封装数据,与数据库表一一对应。文章强调了JavaBean的四个基本要求,并通过User类实例展示了如何创建符合规范的JavaBean。
843

被折叠的 条评论
为什么被折叠?



