javaBean分析
程序员文章站
2022-07-02 17:30:14
在javaWeb开发过程中,javaBean是必须要写的,开一篇关于javaBean的分析整理,长期更新中....... ......
package com.ldf.domain;
/**
* 实体bean
*/
import java.io.serializable;
import java.util.date;
public class user implements serializable{
private int id;
private string username;
private string password;
private string email;
private date birthday;
public user() {
super();
}
public user(int id, string username, string password, string email,
date birthday) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.birthday = birthday;
}
@override
public string tostring() {
return "user [id=" + id + ", username=" + username + ", password="
+ password + ", email=" + email + ", birthday=" + birthday
+ "]";
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this.password = password;
}
public string getemail() {
return email;
}
public void setemail(string email) {
this.email = email;
}
public date getbirthday() {
return birthday;
}
public void setbirthday(date birthday) {
this.birthday = birthday;
}
}
1.实现serializable接口是为了服务器重启或者内存溢出之后,能够实现session的钝化和激活两种状态,钝化表示session数据能够进行存盘操作,激活表示服务器从盘中读取session数据.
public class user implements serializable{
2.字段的定义统一小写且约定必须符合四者的统一,javabean中的字段定义,前端页面的标签name值,后台request.getparameter的形参,以及数据库中字段的定义要保持一致.
private int id;
private string username;
private string password;
private string email;
private date birthday;
3.在不写任何构造器的情况下,系统会默认给出无参构造器,但如果给出了有参构造器,那么必须显式的给出无参构造器.构造器的作用在于初始化参数.
public user() {
super();
}
public user(int id, string username, string password, string email,
date birthday) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.birthday = birthday;
}
4.重写tostring方法,单纯调用对象时,系统会自动调用tostring方法,如果没有重写tostring方法,那么会调用object超级父类的tostring方法,执行结果是该对象的地址值.重写该tostring方法后,系统也会自动调用tostring方法,但是是调用重写后的tostring方法.输出的是自定义的信息.
@override
public string tostring() {
return "user [id=" + id + ", username=" + username + ", password="
+ password + ", email=" + email + ", birthday=" + birthday
+ "]";
}
5.getter/setter方法的作用在于调用封装后的字段,在框架中是具有调用属性的作用.
上一篇: <#assign>