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
        }
    }
  • 相关阅读:
    强引用、软引用、弱引用、幻象引用有什么区别?
    vue基础指令学习
    如何设计一个自动化测试框架
    测试工程师需要了解的shell变量知识
    记一次kubernetes集群异常: kubelet连接apiserver超时
    golang http/transport 代码分析
    logging in kubernetes
    tune kubernetes eviction parameter
    kubernetes continually evict pod when node's inode exhausted
    Compile git version inside go binary
  • 原文地址:https://www.cnblogs.com/ciaos/p/3483414.html
Copyright © 2011-2022 走看看