English 中文(简体)
object. clone () 需要明确刻出吗?
原标题:Shouldn t Object.clone() need explicit cast?
  • 时间:2012-05-22 14:19:23
  •  标签:
  • java
  • clone

为什么 < enger > doesn t object. clone () 需要明确标记? 这不是“ 下播总是需要和明确标记” 规则的例外吗? 我用 JDK1.6.0_29 编译并成功运行了以下代码, 使用 Javac 命令线和 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
最佳回答

您不能调用 Object.clone () 。 您调用 T[].clone () , 以返回 T[]

JLS 10.7 阵列成员 :

数组类型的成员全部如下:

  • 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[].
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签