标签:
范例:
1 public class ImmuteDemo { 2 private String name; //String 为不可变类 3 private Date date; //Date 为可变类 4 5 public ImmuteDemo(String name,Date date){ 6 this.name = name; 7 this.date = new Date(date.getTime()); //对输入对象,进行拷贝 8 // date.setTime(2101); 改变输入对象,并不影响成员date的值 9 } 10 11 public String getName(){ 12 return name; 13 } 14 15 public Date getDate(){ 16 return new Date(date.getTime()); 17 } 18 19 public static void main(String[] args) { 20 Date date = new Date(); 21 ImmuteDemo immuteDemo = new ImmuteDemo("zhang san",date); 22 } 23 } 24
标签:
原文地址:http://www.cnblogs.com/liuyunAlex/p/5189151.html