编程语言
首页 > 编程语言> > Java之拷贝

Java之拷贝

作者:互联网

定义

原理

使用场景

作用

优点

Java中实现拷贝的条件

拷贝的分类

实例之深复制

	  static class Sheep implements Serializable {
	       private static final long serialVersionUID = 1L;
	
	       private String name;
	
	        private Integer age;
	
	        private String  color;
	
	        private Sheep   friend;
	
	       public Sheep getFriend() {
	           return friend;
	       }
	
	       public void setFriend(Sheep friend) {
	           this.friend = friend;
	       }
	
	       public String getName() {
	            return name;
	        }
	
	        public void setName(String name) {
	            this.name = name;
	        }
	
	        public Integer getAge() {
	            return age;
	        }
	
	        public void setAge(Integer age) {
	            this.age = age;
	        }
	
	        public String getColor() {
	            return color;
	        }
	
	        public void setColor(String color) {
	            this.color = color;
	        }
	
	
	      
	
	       /**
	        * 深复制  采用流的方式写入和读取
	        * @return
	        * @throws IOException
	        * @throws ClassNotFoundException
	        */
	        public Object deepClone() throws IOException,ClassNotFoundException{
	            //通过输出流进行数据的写入
	            ByteArrayOutputStream bos=new ByteArrayOutputStream();
	            ObjectOutputStream    oos=new ObjectOutputStream(bos);
	            oos.writeObject(this);
	
	            //通过输入流进行读取
	            ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
	            ObjectInputStream    ois=new ObjectInputStream(bis);
	            Object obj = ois.readObject();
	            bos.flush();
	            bos.close();
	            oos.close();
	            bis.close();
	            ois.close();
	            return  obj;
	
	
	        }
	
	       @Override
	       public String toString() {
	           return "Sheep{" +
	                   "name='" + name + '\'' +
	                   ", age=" + age +
	                   ", color='" + color + '\'' +
	                   ", friend=" + friend +
	                   '}';
	       }
	   }
  


  public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
  
          Sheep s=new Sheep();
    s.setName("ss");


    s.friend=new Sheep();
    s.friend.setName("喜洋洋");

    Sheep s1= (Sheep) s.deepClone();
    s.setName("原数据修改name");
    s.friend.setName("你是谁");


    System.out.println("原对象:"+s+"  复制对象:"+s1);
    System.out.println("原对象:"+s.toString()+"  复制对象:"+s1.toString());
  
  }

 //打印信息
 原对象:  Sheep{name='原数据修改name', age=null, color='null', friend=Sheep{name='你是谁', age=null, color='null', friend=null}}
 复制对象:Sheep{name='复制对象修改数据', age=null, color='null', friend=Sheep{name='喜洋洋', age=null, color='null', friend=null}}

实例之浅复制

	   static class Sheep implements Cloneable {
	
	       private String name;
	
	        private Integer age;
	
	        private String  color;
	
	        private Sheep   friend;
	
	       public Sheep getFriend() {
	           return friend;
	       }
	
	       public void setFriend(Sheep friend) {
	           this.friend = friend;
	       }
	
	       public String getName() {
	            return name;
	        }
	
	        public void setName(String name) {
	            this.name = name;
	        }
	
	        public Integer getAge() {
	            return age;
	        }
	
	        public void setAge(Integer age) {
	            this.age = age;
	        }
	
	        public String getColor() {
	            return color;
	        }
	
	        public void setColor(String color) {
	            this.color = color;
	        }
	
	
	        @NonNull
	        @Override
	        protected Sheep clone() throws CloneNotSupportedException {
	
	            return (Sheep) super.clone();
	        }
	
	       /**
	        * 深复制  采用流的方式写入和读取
	        * @return
	        * @throws IOException
	        * @throws ClassNotFoundException
	        */
	        public Object deepClone() throws IOException,ClassNotFoundException{
	            //通过输出流进行数据的写入
	            ByteArrayOutputStream bos=new ByteArrayOutputStream();
	            ObjectOutputStream    oos=new ObjectOutputStream(bos);
	            oos.writeObject(this);
	
	            //通过输入流进行读取
	            ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
	            ObjectInputStream    ois=new ObjectInputStream(bis);
	            Object obj = ois.readObject();
	            bos.flush();
	            bos.close();
	            oos.close();
	            bis.close();
	            ois.close();
	            return  obj;
	
	
	        }
	
	       @Override
	       public String toString() {
	           return "Sheep{" +
	                   "name='" + name + '\'' +
	                   ", age=" + age +
	                   ", color='" + color + '\'' +
	                   ", friend=" + friend +
	                   '}';
       }
   }
 
   public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {



    Sheep s=new Sheep();
    s.setName("ss");


    s.friend=new Sheep();
    s.friend.setName("喜洋洋");

    Sheep s1= (Sheep) s.clone();
    s.setName("原数据修改name");
    s.friend.setName("你是谁");
    s1.setName("复制对象修改数据");


    System.out.println("原对象:"+s.toString());
    System.out.println("复制对象:"+s1.toString());
    
    
    }
    
    
    //打印信息
   原对象:Sheep{name='原数据修改name', age=null, color='null', friend=Sheep{name='你是谁', age=null, color='null', friend=null}}
   复制对象:Sheep{name='复制对象修改数据', age=null, color='null', friend=Sheep{name='你是谁', age=null, color='null', friend=null}}

标签:Sheep,Java,name,对象,color,拷贝,public,friend
来源: https://blog.csdn.net/meijing11/article/details/119065387