码迷,mamicode.com
首页 > 编程语言 > 详细

java浅拷贝与深拷贝详解

时间:2016-05-08 01:28:32      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:java   clone   

技术分享

/**

 * @author MaskedMen

 *java深克隆与浅克隆讲解

 *技术交流群:JAVA/WEB/安卓精英群 316278345

 *联系邮箱:maskedmenhxy@qq.com

 */

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

 *联系邮箱:maskedmenhxy@qq.com

 */

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

本文出自 “MaskedMen” 博客,请务必保留此出处http://maskedmen.blog.51cto.com/8100866/1771026

java浅拷贝与深拷贝详解

标签:java   clone   

原文地址:http://maskedmen.blog.51cto.com/8100866/1771026

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!