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

  

猜你喜欢

转载自www.cnblogs.com/lianghong881018/p/11244490.html
今日推荐