java 中的 clone()

java 中的 clone()
clone() 是 Object 类中的函数

函数原型:

protected Object clone() throws CloneNotSupportedException

作用:java 中没有拷贝构造函数,所以有 clone()

我们可以自己写一个类,实现 Cloneable 接口
package test_clone;

public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {
    
    private double side;
    
    public Octagon() {
        this(1);
    }

    public Octagon(double side) {
        super();
        this.side = side;
    }
    
    public void setSide(double side) {
        this.side = side;
    }

    public double getArea() {
        return (2 + 4 / Math.sqrt(2)) * this.side * this.side;
    }
    
    public double getPerimeter() {
        return 8 * this.side;
    }

    @Override
    public int compareTo(Octagon o) {
        return (int) (this.side - o.side);
    }
    
    @Override
    public Octagon clone() {
        Octagon octagon = new Octagon();
        octagon.setSide(this.side - 1);
        return octagon;
    }
    
    @Override
    public String toString() {
        return "Octagon [side=" + side + "]";
    }

其中 GeometricObject 是自定义的一个类,如下

package test_clone;

public class GeometricObject {
    private String color;
    private boolean filled;
    
    public GeometricObject() {
        this.color = "white";
        this.filled = false;
    }

    public GeometricObject(String color, boolean filled) {
        super();
        this.color = color;
        this.filled = filled;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
    }

    @Override
    public String toString() {
        return "GeometricObject [color=" + color + ", filled=" + filled + "]";
    }
    
}

我们在类 Octagon 中可以不用覆盖 clone() 函数,即不写:

@Override
    public Octagon clone() {
        Octagon octagon = new Octagon();
        octagon.setSide(this.side - 1);
        return octagon;
    }

那么当调用 clone() 时,是默认的值的复制,我们覆盖后可以让 side 减1。

测试代码
package test_clone;

import java.util.Arrays;

public class TestClone {

    public static void main(String[] args) {
    
        Octagon octagon1 = new Octagon(5);
        Octagon octagon2 = octagon1.clone();
        System.out.println(octagon1.toString());
        System.out.println(octagon2.toString());
    }

}

运行结果:
结果截图

补充

数组是默认实现了 Cloneable 接口的。
例如:

		int[] a = new int[] {1,2,3,4,5};
        int[] b = a.clone();
        System.out.println(Arrays.toString(b));

结果:
在这里插入图片描述
说明:Arrays.toString(b) 能够将数组 b 中元素如图输出。

下面是重点:
那么对于实现了 Cloneable 接口并覆盖了 clone() 函数的类 Octagon 来讲,对 Octagon 的对象数组调用 clone() 会发生什么?

public static void main(String[] args) {
        
        Octagon octagon1 = new Octagon(5);
        Octagon octagon2 = octagon1.clone();
        System.out.println(octagon1.toString());
        System.out.println(octagon2.toString());
        
        Octagon[] octagons1 = new Octagon[3]; 
        for (int i = 0; i < octagons1.length; i++) {
            octagons1[i] = new Octagon(i + 1);
        }
        Octagon[] octagons2 = octagons1.clone();
        System.out.println(Arrays.toString(octagons1));
        System.out.println(Arrays.toString(octagons2));
    }

输出:
在这里插入图片描述
由 octagons1 数组克隆得到的 octagons2 数组中的值没有对应 减1
官方文档解释是:
Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a “shallow copy” of this object, not a “deep copy” operation.

对象数组调用 clone 方法时,用的是浅拷贝。

以上均为个人见解,结果均在 win10:eclipse 上运行所得,如有错误,欢迎指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值