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
        }
    }
  • 相关阅读:
    [na]wac无线控制器集中转发部署的几种情况
    [na]windows2008-AD域的安装
    [na]数据链路层&网络层协议小结截图版
    [na]tcp&udp层各协议小结
    [na]交换机接口文档
    [na]二层sw数据交换
    [na]wireshark排查打印机问题
    [na]ip包格式
    [na]ping提示&各系统默认的TTL值
    【VS开发】C++异常处理操作
  • 原文地址:https://www.cnblogs.com/ciaos/p/3483414.html
Copyright © 2011-2022 走看看