克隆绵羊 没爹没娘。。。。。。。。。。哈哈。。。。。。。。。。。。。。。。。。。。。
对象的克隆 就是对象的拷贝 分为深拷贝 和浅拷贝。 这个方法是(object)类的protected属性的方法。现在说说clone对对象属性的保护。看下面的程序。
fatherclass.java( )
package net.csdn.blog;
import java.util.Date;
class fatherclass {
private String Lastname = null;
private String Firstname = null;
private Date birthday;
public Date getBirthday() {
return (Date) birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getFirstname() {
return Firstname;
}
public void setFirstname(String firstname) {
Firstname = firstname;
}
public String getLastname() {
return Lastname;
}
public void setLastname(String lastname) {
Lastname = lastname;
}
}
sonclass.java
package net.csdn.blog;
import java.util.Date;
public class sonclass extends fatherclass {
/**
* @param args
*/
public static void main (String args[]) {
sonclass son=new sonclass();
son.setFirstname("weikun");
son.setLastname("sun");
son.setBirthday(new Date());
System.out.println("birteday"+son.getBirthday());
double changed=365*10*24*60*60*1000;
Date date=son.getBirthday();
date.setTime(date.getTime()-(long)changed);
System.out.println("chanaged birthday"+son.getBirthday());
}
}
运行结果如下:
birtedaySun Oct 22 15:08:28 CST 2006
chanaged birthdaySun Oct 01 11:32:01 CST 2006
我们清楚地看到,我们并没有调用.setBirday()方法,却带来了我们不想要的改变。破坏了封装性。
如果我们把 fatherclass的setBirthday() 方法改成如下的代码:
public Date getBirthday() {
return (Date) birthday.clone();
}
在此运行程序就能得到两个相同的输出。
浅拷贝:如果原始对象与浅克隆对象共享的信息是不可变的,如原始对象的数据域 是数值或者基本类型。那么浅拷贝没有任何问题。但是如果原始对象包含可变的子对象,那么就会因为信息的共享 而产生连带的改变。克隆对象子对象的改变 会引发原始对象子对象的改变。如下面的程序
package net.csdn.blog;
import java.util.Date;
class Fatherclass implements Cloneable {
private String Lastname = null;
private String Firstname = null;
private Date birthday;
public Date getBirthday() {
return (Date) birthday;
}
public Fatherclass clone() throws CloneNotSupportedException {
return (Fatherclass) super.clone();
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getFirstname() {
return Firstname;
}
public void setFirstname(String firstname) {
Firstname = firstname;
}
public String getLastname() {
return Lastname;
}
public void setLastname(String lastname) {
Lastname = lastname;}
public static void main(String args[]) throws CloneNotSupportedException {
Fatherclass father = new Fatherclass();
father.setFirstname("aaa");
father.setLastname("sun");
father.setBirthday(new Date());
Date d;
d=father.clone().getBirthday();
int a=1000000000;
d.setTime((long)a);
System.out.println(d);
System.out.println(father.getBirthday());
}
}
他的运行结果就是
Mon Jan 12 21:46:40 CST 1970
Mon Jan 12 21:46:40 CST 1970
如果我们采用深拷贝 就不会了。看如下的程序:
package net.csdn.blog;
import java.util.Date;
class Fatherclass implements Cloneable {
private String Lastname = null;
private String Firstname = null;
private Date birthday;
public Date getBirthday() {
return (Date) birthday;
}
public Fatherclass clone() throws CloneNotSupportedException {
Fatherclass cloned=(Fatherclass) super.clone();
cloned.birthday=(Date) birthday.clone();
return cloned;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getFirstname() {
return Firstname;
}
public void setFirstname(String firstname) {
Firstname = firstname;
}
public String getLastname() {
return Lastname;
}
public void setLastname(String lastname) {
Lastname = lastname;}
public static void main(String args[]) throws CloneNotSupportedException {
Fatherclass father = new Fatherclass();
father.setFirstname("aaa");
father.setLastname("sun");
father.setBirthday(new Date());
Date d;
d=father.clone().getBirthday();
int a=1000000000;
d.setTime((long)a);
System.out.println(d);
System.out.println(father.getBirthday());
}
他的运行结果就是
Mon Jan 12 21:46:40 CST 1970
Sun Oct 22 17:12:29 CST 2006
请注意 clone 方法。不仅仅克隆了原始对象,也克隆了原始对象的可变子对象。