java - 为什么数组允许使用克隆方法?

标签 java clone

clone() 方法默认在 Object 类中不可见,那么它如何不对数组类型给出错误?
这是否意味着有一个名为 int[] 的类型,其实现是用 java 编写的,如果是,在哪里可以找到它?
既然已经写了,为什么不完整地写出来呢?
我的意思是为什么不正确实现每个方法而不仅仅是 Object 类的行为。

    int[] a ={1,2,3};
    Object object = new Object();
    object.clone();//Does not compile since clone is protected.
    a.clone();// allowed as this method is from int[] 

最佳答案

所有数组都实现 Cloneable 接口(interface)。

请参阅 Java 语言规范的相关部分:§10.7 Array Members

The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[]. A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

关于java - 为什么数组允许使用克隆方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086115/

相关文章:

java - 想要动态更改 gridbag 布局中的 Inset

javascript - 使用纯 JavaScript 克隆字段集并更改 ID?

javascript - Ext JS 4 - 克隆节点并附加到 DOM 中的其他位置

java - 转换不可修改的集合

java - 将 java.util.List<String> 转换为 java.sql.Array

java - 从枚举序数转换为枚举类型

java - 为什么我的程序在没有相应的打印语句的情况下打印值?

java - 是否可以使用 clon() 方法而不覆盖它

javascript - 克隆元素 N 次,具体取决于选择选项值