Java面试_克隆

一、基本克隆

package za;

public class CloneExample {
    private int a;
    private int b;
    @Override
    public CloneExample clone() throws CloneNotSupportedException{
        return (CloneExample)super.clone();
    }

    public static void main(String[] args) {
        CloneExample e1 = new CloneExample();
        try{
            CloneExample e2 = e1.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
    }
}

二、浅拷贝

package za;

public class ShallowCloneExample implements Cloneable{
    private int[] a;
    public ShallowCloneExample(){
      a = new int[15];
        for (int i = 0; i < a.length; i++) {
            a[i]=i;
        }
    }
    public void set(int index,int value){
        a[index]=value;
    }
    public int get(int index){
        return a[index];
    }
    @Override
    protected ShallowCloneExample clone() throws CloneNotSupportedException{
        return (ShallowCloneExample)super.clone();
    }

    public static void main(String[] args) {
        ShallowCloneExample e1 = new ShallowCloneExample();
        ShallowCloneExample e2 = null;
        try {
            e2 = e1.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        e1.set(2,1230);
        System.out.println(e2.get(2));//1230
    }
}

三、深拷贝

package za;

public class DeepCloneExample implements Cloneable{
    private int[] a;
    public DeepCloneExample(){
        a = new int[15];
        for (int i = 0; i < a.length; i++) {
            a[i]=i;
        }
    }
    public void set(int index,int value){
        a[index]=value;
    }
    public int get(int index){
        return a[index];
    }
    @Override
    protected DeepCloneExample clone() throws CloneNotSupportedException{
        DeepCloneExample result = (DeepCloneExample)super.clone();
        result.a = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            result.a[i] = a[i]+1;
        }
        return  result;
    }
    public static void main(String[] args) {
        DeepCloneExample e1 = new DeepCloneExample();
        DeepCloneExample e2 = null;
        try {
            e2 = e1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        e1.set(2, 222);
        System.out.println(e2.get(2)); // 2+1
    }
}

四、 clone() 的替代方案

package za;

public class CloneConstructorExample {
    private int[] a;
    public CloneConstructorExample(){
        a = new int[15];
        for (int i = 0; i < a.length; i++) {
            a[i]=i;
        }
    }
    public CloneConstructorExample(CloneConstructorExample ce){
        a = new int[ce.a.length];
        for (int i = 0; i < ce.a.length; i++) {
            a[i]=ce.a[i];
        }
    }
    public void set(int index,int value){
        a[index]=value;
    }
    public int get(int index){
        return a[index];
    }

    public static void main(String[] args) {
        CloneConstructorExample e1 = new CloneConstructorExample();
        CloneConstructorExample e2 = new CloneConstructorExample(e1);
        e1.set(2, 222);
        System.out.println(e2.get(2)); // 2
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值