zoukankan      html  css  js  c++  java
  • java对象复制

    一,a和b都指向同一个对象,改变其中一个另一个也会改变

    package com.ciaos;
    
    class Human{
        public Human(String string, int i) {
            // TODO Auto-generated constructor stub
            name = string;
            age = i;
        }
        String name;
        int age;
    }
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    /*        int num[][] = {{1,2,3,4},{5,6,7,8},{2,3,4,5},{4,5,6,7}};
            for(int x[]:num){
                for(int y:x){
                    System.out.print(y);
                }
            }*/
            Human a = new Human("ciaos",26);
            System.out.println("a:"+a.name+" "+a.age);//a:ciaos 26
            
            Human b = a;
            b.name = "stone";
            System.out.println("a:"+a.name+" "+a.age);//a:stone 26
            System.out.println("b:"+b.name+" "+b.age);//b:stone 26
        }
    }

    二,继承Cloneable接口,实现clone方法,实现浅拷贝

    package com.ciaos;
    
    class Human implements Cloneable{
        public Human(String string, int i) {
            // TODO Auto-generated constructor stub
            name = string;
            age = i;
        }
        String name;
        int age;
        
        public Object clone(){
            Human h = null;
            try {
                h = (Human)super.clone();
            } catch (CloneNotSupportedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return h;
        }
    }
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Human a = new Human("ciaos",26);
            System.out.println("a:"+a.name+" "+a.age);//a:ciaos 26
            
            Human b = (Human)a.clone();
            b.name = "stone";
            System.out.println("a:"+a.name+" "+a.age);//a:ciaos 26
            System.out.println("b:"+b.name+" "+b.age);//b:stone 26
        }
    }

    三,如果对象中有变量指向别的对象,成员变量指向的对象仍然是同样的

    package com.ciaos;
    
    class Addr{
        public Addr(String country2, String city2) {
            // TODO Auto-generated constructor stub
            country = country2;
            city = city2;
        }
        String country;
        String city;
    }
    
    class Human implements Cloneable{
        public Human(String string, int i, String country, String city) {
            // TODO Auto-generated constructor stub
            name = string;
            age = i;
            addr = new Addr(country,city);
        }
        String name;
        int age;
        Addr addr;
        
        public Object clone(){
            Human h = null;
            try {
                h = (Human)super.clone();
            } catch (CloneNotSupportedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return h;
        }
    }
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Human a = new Human("ciaos",26,"china","shenzhen");
            System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shenzhen
            
            Human b = (Human)a.clone();
            b.name = "stone";
            b.addr.city = "shanghai";
            System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shanghai
            System.out.println("b:"+b.name+" "+b.age+" "+b.addr.country+" "+b.addr.city);//b:stone 26 china shanghai
        }
    }

    四,需要对成员变量指向对象的类继承拷贝接口,修改Human类复制方法

    package com.ciaos;
    
    class Addr implements Cloneable{
        public Addr(String country2, String city2) {
            // TODO Auto-generated constructor stub
            country = country2;
            city = city2;
        }
        String country;
        String city;
        public Object clone(){
            Addr a = null;
            try {
                a = (Addr)super.clone();
            } catch (CloneNotSupportedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return a;
        }
    }
    
    class Human implements Cloneable{
        public Human(String string, int i, String country, String city) {
            // TODO Auto-generated constructor stub
            name = string;
            age = i;
            addr = new Addr(country,city);
        }
        String name;
        int age;
        Addr addr;
        
        public Object clone(){
            Human h = null;
            try {
                h = (Human)super.clone();
                h.addr = (Addr)this.addr.clone();
            } catch (CloneNotSupportedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return h;
        }
    }
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Human a = new Human("ciaos",26,"china","shenzhen");
            System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shenzhen
            
            Human b = (Human)a.clone();
            b.name = "stone";
            b.addr.city = "shanghai";
            System.out.println("a:"+a.name+" "+a.age+" "+a.addr.country+" "+a.addr.city);//a:ciaos 26 china shenzhen
            System.out.println("b:"+b.name+" "+b.age+" "+b.addr.country+" "+b.addr.city);//b:stone 26 china shanghai
        }
    }
  • 相关阅读:
    Python 运算符优先级
    一起谈.NET技术,HubbleDotNet 和 Lucene.Net 匹配相关度的比较 狼人:
    一起谈.NET技术,Asp优化,asp缓存技术 狼人:
    一起谈.NET技术,MonoTouch中的MVC简介 狼人:
    一起谈.NET技术,如何成为人尽皆知的C#开发人员 狼人:
    一起谈.NET技术,如何解决分布式系统中的跨时区问题[原理篇] 狼人:
    一起谈.NET技术,WCF使用NetTcp传输文件 狼人:
    一起谈.NET技术,ASP.NET 项目安装包制作(二)数据库安装、其他组件的安装 狼人:
    一起谈.NET技术,如何解决分布式系统中的跨时区问题[实例篇] 狼人:
    一起谈.NET技术,模拟IIS向Silverlight输出策略文件 狼人:
  • 原文地址:https://www.cnblogs.com/ciaos/p/3483414.html
Copyright © 2011-2022 走看看