/**
* @author MaskedMen
*java深克隆与浅克隆讲解
*技术交流群:JAVA/WEB/安卓精英群 316278345
*联系邮箱:[email protected]
*/
public class CloneDemo {
public static void main(String[] args) {
Person maskedmen1 = new Person("maskedmen1",20);
Fruit watermelon = new Fruit("西瓜",maskedmen1);
System.out.println("****************浅拷贝之基本数据类型可以改变并不影响前一个对象的属性值******************");
Person m1 = new Person("m1",21);
try {
Person m2 = (Person) m1.clone();
System.out.println("浅拷贝原始对象"+m1.toString());
//浅拷贝原始对象Person [name=m1, age=21]
System.out.println("浅拷贝后的对象"+m2.toString());
//浅拷贝后的对象Person [name=m1, age=21]
System.out.println("改变原始对象m1的年龄值:");
m1.setAge(23);
System.out.println("改变原始对象m1的年龄值m2的输出结果"+m2.toString());
//改变原始对象m1的年龄值m2的输出结果Person [name=m1, age=21]
System.out.println("改变原始对象m1的年龄值后m1对象的输出结果"+m1.toString());
//改变原始对象m1的年龄值后m1对象的输出结果Person [name=m1, age=23]
} catch (CloneNotSupportedException e1) {
e1.printStackTrace();
}
System.out.println("*************浅拷贝之子对象属于共享,改变后会影响前一个对象的属性值************");
try {
System.out.println("浅拷贝操作前的原始对象"+watermelon.toString());
//浅拷贝操作前的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
Fruit potato = watermelon.clone();
System.out.println("浅拷贝的对象"+potato.toString());
//浅拷贝的对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
System.out.println("改变原始对象子类对象的值");
maskedmen1.setName("maskedmen2");
maskedmen1.setAge(22);
System.out.println("浅拷贝操作后的原始对象"+watermelon.toString());
//浅拷贝操作后的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen2, age=22]]
System.out.println("原始对象改变之后的浅拷贝对象"+potato.toString());
//原始对象改变之后的浅拷贝对象Fruit [fName=西瓜, person=Person [name=maskedmen2, age=22]]
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
/* System.out.println("*****************深拷贝之原始子类对象的改变不影响拷贝对象*****************************");
try {
System.out.println("深拷贝操作前的原始对象"+watermelon.toString());
//深拷贝操作前的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
Fruit potato = watermelon.clone();
System.out.println("深拷贝对象"+potato.toString());
//深拷贝对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
System.out.println("改变原始对象子类对象的值");
maskedmen1.setName("maskedmen3");
maskedmen1.setAge(23);
System.out.println("更改子类对象操作后的原始对象"+watermelon.toString());
//更改子类对象操作后的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen3, age=23]]
System.out.println("更改子类对象操作后的拷贝对象"+potato.toString());
//更改子类对象操作后的拷贝对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
*/
}
}
//水果深拷贝测试类
class Fruit implements Cloneable{
private String fName;//水果名称
private Person person ;//水果所属人
/* @Override//重写clone方法 实现深拷贝
public Fruit clone() throws CloneNotSupportedException {
Fruit potato = (Fruit) super.clone();//确定要克隆的对象
potato.person=(Person) person.clone();//实现子对象的克隆
return potato;
}*/
@Override//重写clone方法 实现浅拷贝
public Fruit clone() throws CloneNotSupportedException {
return (Fruit) super.clone();
}
public Fruit() {
}
public Fruit(String fName, Person person) {
super();
this.fName = fName;
this.person = person;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Fruit [fName=" + fName + ", person=" + person + "]";
}
}
//person测试类
class Person implements Cloneable{
private String name;//人的姓名
private int age;//年龄
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}/**
* @author MaskedMen
*java深克隆与浅克隆讲解
*技术交流群:JAVA/WEB/安卓精英群 316278345
*联系邮箱:[email protected]
*/
public class CloneDemo {
public static void main(String[] args) {
Person maskedmen1 = new Person("maskedmen1",20);
Fruit watermelon = new Fruit("西瓜",maskedmen1);
System.out.println("****************浅拷贝之基本数据类型可以改变并不影响前一个对象的属性值******************");
Person m1 = new Person("m1",21);
try {
Person m2 = (Person) m1.clone();
System.out.println("浅拷贝原始对象"+m1.toString());
//浅拷贝原始对象Person [name=m1, age=21]
System.out.println("浅拷贝后的对象"+m2.toString());
//浅拷贝后的对象Person [name=m1, age=21]
System.out.println("改变原始对象m1的年龄值:");
m1.setAge(23);
System.out.println("改变原始对象m1的年龄值m2的输出结果"+m2.toString());
//改变原始对象m1的年龄值m2的输出结果Person [name=m1, age=21]
System.out.println("改变原始对象m1的年龄值后m1对象的输出结果"+m1.toString());
//改变原始对象m1的年龄值后m1对象的输出结果Person [name=m1, age=23]
} catch (CloneNotSupportedException e1) {
e1.printStackTrace();
}
System.out.println("*************浅拷贝之子对象属于共享,改变后会影响前一个对象的属性值************");
try {
System.out.println("浅拷贝操作前的原始对象"+watermelon.toString());
//浅拷贝操作前的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
Fruit potato = watermelon.clone();
System.out.println("浅拷贝的对象"+potato.toString());
//浅拷贝的对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
System.out.println("改变原始对象子类对象的值");
maskedmen1.setName("maskedmen2");
maskedmen1.setAge(22);
System.out.println("浅拷贝操作后的原始对象"+watermelon.toString());
//浅拷贝操作后的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen2, age=22]]
System.out.println("原始对象改变之后的浅拷贝对象"+potato.toString());
//原始对象改变之后的浅拷贝对象Fruit [fName=西瓜, person=Person [name=maskedmen2, age=22]]
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
/* System.out.println("*****************深拷贝之原始子类对象的改变不影响拷贝对象*****************************");
try {
System.out.println("深拷贝操作前的原始对象"+watermelon.toString());
//深拷贝操作前的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
Fruit potato = watermelon.clone();
System.out.println("深拷贝对象"+potato.toString());
//深拷贝对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
System.out.println("改变原始对象子类对象的值");
maskedmen1.setName("maskedmen3");
maskedmen1.setAge(23);
System.out.println("更改子类对象操作后的原始对象"+watermelon.toString());
//更改子类对象操作后的原始对象Fruit [fName=西瓜, person=Person [name=maskedmen3, age=23]]
System.out.println("更改子类对象操作后的拷贝对象"+potato.toString());
//更改子类对象操作后的拷贝对象Fruit [fName=西瓜, person=Person [name=maskedmen1, age=20]]
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
*/
}
}
//水果深拷贝测试类
class Fruit implements Cloneable{
private String fName;//水果名称
private Person person ;//水果所属人
/* @Override//重写clone方法 实现深拷贝
public Fruit clone() throws CloneNotSupportedException {
Fruit potato = (Fruit) super.clone();//确定要克隆的对象
potato.person=(Person) person.clone();//实现子对象的克隆
return potato;
}*/
@Override//重写clone方法 实现浅拷贝
public Fruit clone() throws CloneNotSupportedException {
return (Fruit) super.clone();
}
public Fruit() {
}
public Fruit(String fName, Person person) {
super();
this.fName = fName;
this.person = person;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public String toString() {
return "Fruit [fName=" + fName + ", person=" + person + "]";
}
}
//person测试类
class Person implements Cloneable{
private String name;//人的姓名
private int age;//年龄
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
源码下载地址:http://pan.baidu.com/s/1o7SJoKU