javaBean分析

Octopus66發表於2018-12-13
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方法的作用在於呼叫封裝後的欄位,在框架中是具有呼叫屬性的作用.

相關文章