java中的clone()方法的研究---(7)如何编写正确的clone()方法:Date, Timestamp

来源:互联网 发布:什么贵金属软件好 编辑:程序博客网 时间:2024/06/11 00:18

五:Date, Timestamp类型


在自定义类Person中添加新的属性:Date, Timestamp


package tt.vo;import java.sql.Timestamp;import java.util.Arrays;import java.util.Date;public class Person implements Cloneable {// 基本数据类型private int age;// Wrapper Class类型private Integer height;// String 类型private String name;// StringBufferprivate StringBuffer address1;// StringBuilderprivate StringBuilder address2;// Dateprivate Date date;// Timestampprivate Timestamp timestamp;@Overridepublic Person clone() throws CloneNotSupportedException {Person p = (Person) super.clone();// StringBuffer,StringBuilder 没有实现clone方法// 只能用 newp.address1 = new StringBuffer(this.address1);p.address2 = new StringBuilder(this.address2);p.date = (Date)this.date.clone();p.timestamp = (Timestamp)this.timestamp.clone();return p;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Integer getHeight() {return height;}public void setHeight(Integer height) {this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public StringBuffer getAddress1() {return address1;}public void setAddress1(StringBuffer address1) {this.address1 = address1;}public StringBuilder getAddress2() {return address2;}public void setAddress2(StringBuilder address2) {this.address2 = address2;}@Overridepublic String toString() {return "Person [age=" + age + ", height=" + height + ", name=" + name+ ", address1=" + address1 + ", address2=" + address2+ ", date=" + date + ", timestamp=" + timestamp + "]";}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public Timestamp getTimestamp() {return timestamp;}public void setTimestamp(Timestamp timestamp) {this.timestamp = timestamp;}}


注意,这里的clone()方法,针对Date, Timestamp这两种类型的克隆,我是直接贴出来正确的写法。

只有调用date.clone(),timestamp.clone() 才是深克隆。


@Overridepublic Person clone() throws CloneNotSupportedException {Person p = (Person) super.clone();// StringBuffer,StringBuilder 没有实现clone方法// 只能用 newp.address1 = new StringBuffer(this.address1);p.address2 = new StringBuilder(this.address2);p.date = (Date)this.date.clone(); // Date 有clone方法p.timestamp = (Timestamp)this.timestamp.clone();  // Timestamp 有clone方法return p;}


---------------------------------------------------------------下面开始测试了------------------------------------------------------------------

测试类TestMain:


package tt;import java.sql.Timestamp;import java.util.Date;import tt.vo.Person;public class TestMain {public static void main(String[] args) throws CloneNotSupportedException {// initialize Person pPerson p = new Person();// int p.setAge(18);// Integerp.setHeight(162);// Stringp.setName("郭美美");// StringBufferp.setAddress1(new StringBuffer("中国北京"));// StringBuilderp.setAddress2(new StringBuilder("美国纽约"));// Datep.setDate(new Date());// Timestampp.setTimestamp(new Timestamp(123456789l));System.out.println("p:" + p);Person pclone = p.clone();System.out.println("pclone:" + pclone);System.out.println("-------------after set-------------------");p.setAge(180);p.setHeight(190);p.setName("郭美美不美");pclone.getAddress1().append("!!!");pclone.getAddress2().append("......");p.getDate().setYear(2000);p.getTimestamp().setYear(3000);System.out.println("p:" + p);System.out.println("pclone:" + pclone);}}

debug截图:



成功克隆的pclone.data,pclone.timestamp id 与原来的不同。

修改了 p.getDate().setYear(2000); p.getTimestamp().setYear(3000); 之后,我们来看看控制台打印的结果。

很令人满意~~~


p:Person [age=180, height=190, name=郭美美不美, address1=中国北京, address2=美国纽约, date=Mon Jun 11 16:37:42 CST 3900, timestamp=4900-01-02 18:17:36.789]
pclone:Person [age=18, height=162, name=郭美美, address1=中国北京!!!, address2=美国纽约......,date=Thu Jun 11 16:37:42 CST 2015, timestamp=1970-01-02 18:17:36.789]


0 0
原创粉丝点击