不可变类例子

//不可变类:类初始化后其属性不可改变,例如String、Double属于不可变类。
//做法:1、把属性用private和final修饰 ;2、提供初始化属性的构造器;
//3、只提供get方法不停工set方法
public class Address {
	private final String detail;
	private final String postCode;
	public Address(String detail, String postCode) {
		super();
		this.detail = detail;
		this.postCode = postCode;
	}
	public Address() {
		this.detail ="";
		this.postCode="";
	}
	public String getDetail() {
		return detail;
	}
	public String getPostCode() {
		return postCode;
	}

}

猜你喜欢

转载自barabbas.iteye.com/blog/1833202