javaBean分析

2018-12-13 08:56:03来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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方法的作用在于调用封装后的字段,在框架中是具有调用属性的作用.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何使用 eclipse进行断点 debug 程序

下一篇:java数组创建