为什么Object.clone() 调用不需要显式转换?这不是“向下转换总是需要和显式转换”规则的例外吗?我使用 javac 命令行和带有 JDK1.6.0_29 的 Eclipse Helios 编译并成功运行了以下代码。
public class Main {
public static void main(String[] args) {
byte[] original = { 1, 2, 3, 4 };
byte[] copy = original.clone();
for (byte b : copy) {
System.out.print(b + " ");
}
int[] originalInt = { 11, 22, 33, 44 };
int[] copyInt = originalInt.clone();
for (int i : copyInt) {
System.out.print(i + " ");
}
String[] originalStr = { "1", "2", "3", "4" };
String[] copyStr = originalStr.clone();
for (String s : copyStr) {
System.out.print(s + " ");
}
Main[] originalMain = { new Main() };
Main[] copyMain = originalMain.clone();
for (Main m : copyMain) {
System.out.print(m + " ");
}
} // end method main
} // end class Main