对象克隆...

Posted on 2006-04-23 11:30  CQIT-CS  阅读(1976)  评论(2编辑  收藏  举报


      上面是GOF设计模式中对原型模式的图形结构描述,原型模式通过克隆使我们可以得到一个对象的复制版本.其好处就是让我们在需要一个与现有对象类似的实例时,不用一一进行每个成员的赋值,而是直接通过现有的对象复制.并且复制出来的对象是互相独立的.

       如上图,当对象进行了这样的赋值以后,两个变量指向了同一个对象实例,也就是说它们都引用是相同的,在这种情况下,其中一个对象的改变都会影响另一个对象的变化.两个对象互不独立.



         如果想得到互相独立的两个对象就要使用Clone方法,如上图,经Clone后,任何一个对象的改变都不会影响另一个对象。Object1上Object2是两个不同的引用.
         

      如果一个欲实现Clone方法的对象其成员中还包含其它对象,那么那些对象同样要实现Clone方法,而且在上一层的对象中要调用这些Clone方法.以此类推,直到最后一个对象中没有对象值为止(深拷贝).
     在JAVA中,要实现Clone方法就要实现Cloneable接口,但是Clone方法不是这个接口的方法,它只是一个标识接口。Clone方法是从Object对象继承下来的protected方法,在实现它的时候要声明为public。在使用Clone的时候还要记得进行类型转换,因为Clone方法返回的是一个Object。
     下面是一小段简单的代码,是对对象克隆的实验:
/**
   *ImplClone
   **/
    package exclone;

/**
 *
 * @author Bill
 */
public class ImplClone implements Cloneable{
       
        private String name="";
        private Birth birth;
        /** Creates a new instance of ImplClone */
        public ImplClone() {
                this.setName("bill");
                this.birth=new Birth();
        }
       
        public Object clone(){
                ImplClone cloned=new ImplClone();
                try {
                        cloned = (ImplClone) super.clone();
                        cloned.birth=(Birth)this.birth.clone();
                } catch (CloneNotSupportedException ex) {
                        ex.printStackTrace();
                }
                cloned.birth=(Birth)birth.clone();
                return cloned;
        }

        public String getName() {
                return name;
        }

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

        public Birth getBirth() {
                return birth;
        }

        public void setBirth(Birth birth) {
                this.birth = birth;
        }

}

class Birth implements Cloneable{
        private String year;
        private String month;
        private String day;

        public Birth(String year,String month,String day){
                this.year=year;
                this.month=month;
                this.day=day;
        }
       
        public Birth(){
        }
       
        public String getYear() {
                return year;
        }

        public void setYear(String year) {
                this.year = year;
        }

        public String getMonth() {
                return month;
        }

        public void setMonth(String month) {
                this.month = month;
        }

        public String getDay() {
                return day;
        }

        public void setDay(String day) {
                this.day = day;
        }
       
        public Object clone(){
                Birth cloned=new Birth();
                try {
                        cloned = (Birth) super.clone();
                } catch (CloneNotSupportedException ex) {
                        ex.printStackTrace();
                }
                return cloned;
        }
}

/**
   *ImplClone-MainClass
   **/
    public class CloneMain {
       
        /** Creates a new instance of CloneMain */
        public CloneMain() {
        }
       
        public static void main(String[] args){
                ImplClone cloneObj=new ImplClone();
               
                cloneObj.setName("Bill");
                cloneObj.setBirth(new Birth("2004","03","01"));
               
                System.out.println(cloneObj.getBirth().getYear()+" :1");
                ImplClone cloned=new ImplClone();
                //cloned=cloneObj;
                ImplClone cloned=(ImplClone)cloneObj.clone();
                cloned.getBirth().setYear("1990");       //改变克隆对象的数据
                
                System.out.println(cloned.getBirth().getYear()+" :2");    //输出原对象
                System.out.println(cloned.getBirth().getYear()+" :3");    //输出改变数据后的克隆对象
        }
}

Copyright © 2024 CQIT-CS
Powered by .NET 8.0 on Kubernetes