您的位置:首页 > 其它

建议43:避免对象的浅拷贝

2013-04-06 22:32 120 查看
学习小结:

1、java中为什么会出现拷贝?即拷贝的作用和优势是什么?

2、浅拷贝与深拷贝的区别是什么?

3、实现拷贝时,我们需要注意什么?

1、一般情况下我们是通过new操作符来获取一个属性都是初始值的对象。当若不通过设置其属性,而想得到一个与已有对象一致的对象时,我们该如何处理。高斯林等先驱们考虑周全,提出了clone概念。

拷贝的优势在于实现它的机制,拷贝操作的内存,因而执行速度快,效率高。特别是针对于那些包含多个其他对象的引用的对象来说,比new执行效率高多了。

2、浅拷贝就是仅仅复制所考虑的对象及其基本属性,而不复制它引用的对象。即拷贝后生成对象中的引用对象与原对象的是同一个。即两者还存在公用部分。

深拷贝就是将原对象的所有属性全拷贝一遍。使得两者完全独立。

3、在我的实际应用中还没用到拷贝操作,不过用到了序列化,序列化其实就是实现深拷贝的一种具体实现方式。若想某个对象可以被克隆,需要该类实现Cloneable接口,重写clone方法。

具体使用方式如下(例子是拷贝过来的):

1、浅拷贝的使用实例

public class EveryDay
{
// teacher对象将被clone出来的Student对象共享.

public static void main(String[] args) throws Exception
{
Teacher teacher = new Teacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");

Student student1 = new Student();
student1.setAge(20);
student1.setName("zhangsan");
student1.setTeacher(teacher);

// 复制出来一个对象student2
Student student2 = (Student) student1.clone();
System.out.println(student2.getAge());
System.out.println(student2.getName());
student2.setName("fans");
student2.setAge(0);

System.out.println("modify it "+student2.getName());
System.out.println("modify it "+student1.getName());
System.out.println("modify it "+student2.getAge());
System.out.println("modify it "+student1.getAge());

System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());

// 修改student2的引用对象
student2.getTeacher().setAge(50);
student2.getTeacher().setName("Teacher Li");

System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());
}
}

class Teacher
{
public int age;
public String name;

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}

class Student implements Cloneable
{

public int age;
public String name;
public Teacher teacher;

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public Teacher getTeacher()
{
return teacher;
}

public void setTeacher(Teacher teacher)
{
this.teacher = teacher;
}

@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}

}


输出结果:

20
zhangsan
modify it fans
modify it zhangsan
modify it 0
modify it 20
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
50
Teacher Li


2、深拷贝的使用实例:
public class DeepCloneTest
{

public static void main(String[] args) throws Exception
{
// teacher对象将不被clone出来的Student对象共享.
Teacher1 teacher = new Teacher1();
teacher.setAge(40);
teacher.setName("Teacher zhang");

Student1 student1 = new Student1();
student1.setAge(20);
student1.setName("zhangsan");
student1.setTeacher(teacher);

// 复制出来一个对象student2
Student1 student2 = (Student1) student1.clone();
System.out.println(student2.getAge());
System.out.println(student2.getName());
student2.setName("fans");
student2.setAge(0);

System.out.println("modify it "+student2.getName());
System.out.println("modify it "+student1.getName());
System.out.println("modify it "+student2.getAge());
System.out.println("modify it "+student1.getAge());

System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());

// 修改student2的引用对象
student2.getTeacher().setAge(50);
student2.getTeacher().setName("Teacher Li");

System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());
}
}

class Teacher1
{
public int age;
public String name;

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}

class Student1 implements Cloneable
{

public int age;
public String name;
public Teacher1 teacher;

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public Teacher1 getTeacher()
{
return teacher;
}

public void setTeacher(Teacher1 teacher)
{
this.teacher = teacher;
}

@Override
public Object clone() throws CloneNotSupportedException
{
Student1 student = (Student1) super.clone();
// 将引用的对象teacher也clone下
student.setTeacher(new Teacher1());
return student;
}
}
输出结果:

20
zhangsan
modify it fans
modify it zhangsan
modify it 0
modify it 20
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang


参考网址:
http://kuangbaoxu.iteye.com/blog/193222 http://developer.51cto.com/art/201104/253067.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐