learning java Cloneable

class Address{
    String Detail;
    public Address(String detail){
        this.Detail = detail;
    }
}

class User implements Cloneable{
    int age;
    Address address;

    public  User(int age){
        this.age = age;
        this.address = new Address("houyang");
    }

    public User clone() throws  CloneNotSupportedException{
        return (User) super.clone();
    }
}

public class CloneTest {
    public static void main(String[] args) throws  CloneNotSupportedException {
        var c1 =  new User(18);
        var c2 =  c1.clone();

        System.out.println(c1 == c2);
        System.out.println(c1.address == c2.address);

    }
}

output:

false
true

  

posted @ 2019-07-25 15:21  嵌入式实操  阅读(96)  评论(0编辑  收藏  举报