posts - 37, comments - 8, trackbacks - 0, articles - 0

java基础:关于clone

Posted on 2008-08-02 07:16 梦与桥 阅读(175) 评论(0)  编辑  收藏 所属分类: java基础

1、目的:为了获得对象的一份拷贝。
2、一般步骤:§在派生类中覆盖基类(object)的clone方法,并声明为public
                          §在派生类的克隆方法中调用super.clone()
                          §在派生类中实现Cloneable接口

3、分类:浅拷贝、深拷贝
                    §浅拷贝仅仅拷贝所考虑的对象,而不拷贝它所引用的对象
                    §深拷贝把要拷贝的对象所引用的对象都拷贝了一遍
4、浅拷贝实例:

class Book implements Cloneable
{
 private String name;
 private double price;
 public Author author;

 Book(String name,double price,Author author)
 {
  this.name=name;
  this.price=price;
  this.author=author;
 }
 public String toString()
 {
  return "book's name:"+name+"\t\tbook's price:"+price+author.tostring();
 }
 public Object clone()
 {
  Object o=null;
  try
  {
   o=super.clone();
  }
  catch (CloneNotSupportedException e)
  {
   System.out.println(e.toString());
  }
  return o;
 }
}
class Author
{
 private String name;
 private int age;
 Author(String name,int age)
 {
  this.name=name;
  this.age=age;
 }
 public void set(String name,int age)
 {
  this.name=name;
  this.age=age;
 }
 public String tostring()
 {
  return "\nauthor's name:"+name+"\t\tauthor's age"+age;
 }
}
public class Test
{
 public static void main(String args[])
 {
  Author author=new Author("孙悟空",800);
  Book book1=new Book("高等数学",32.00,author);
  Book book2=(Book)book1.clone();
  book1.author.set("唐僧",700);//此处的修改影响到了book2,体现了浅拷贝的特点
  System.out.println(book2);
 }
}

5、深拷贝实例:

class Book implements Cloneable
{
 private String name;
 private double price;
 public Author author;

 Book(String name,double price,Author author)
 {
  this.name=name;
  this.price=price;
  this.author=author;
 }
 public String toString()
 {
  return "book's name:"+name+"\t\tbook's price:"+price+author.tostring();
 }
 public Object clone()
 {
  Book o=null;
  try
  {
   o=(Book)super.clone();
  }
  catch (CloneNotSupportedException e)
  {
   System.out.println(e.toString());
  }
  o.author=(Author)author.clone();
  return o;
 }
}
class Author implements Cloneable
{
 private String name;
 private int age;
 Author(String name,int age)
 {
  this.name=name;
  this.age=age;
 }
 public void set(String name,int age)
 {
  this.name=name;
  this.age=age;
 }
 public String tostring()
 {
  return "\nauthor's name:"+name+"\t\tauthor's age"+age;
 }
  public Object clone()
 {
  Object o=null;
  try
  {
   o=super.clone();
  }
  catch (CloneNotSupportedException e)
  {
   System.out.println(e.toString());
  }
  return o;
 }
}
public class Test
{
 public static void main(String args[])
 {
  Author author=new Author("孙悟空",800);
  Book book1=new Book("高等数学",32.00,author);
  Book book2=(Book)book1.clone();
  book1.author.set("唐僧",700);//此处的修改没有影响book2,体现了深拷贝的特点。
  System.out.println(book2);
 }
}

6、注意:
            §在派生类中覆盖Object的clone()方法时,一定要调用super.clone(),因为在运行时刻,Object中的clone()识别出你要复制的是哪一个对象,然后为此对象分配空间,并进行对象的复制,将原始对象的内容一一复制到新对象的存储空间中。
            §在Object.clone()正式开始操作前,首先会检查一个类是否Cloneable,即是否具有克隆能力——换言之,它是否实现了Cloneable接口。若未实现,Object.clone()就掷出一个CloneNotSupportedException违例,指出我们不能克隆它。

只有注册用户登录后才能发表评论。


网站导航: