java 中的克隆()

发布于 2025-01-02 22:52:21 字数 230 浏览 2 评论 0原文

import java.util.*;
import java.lang.*;

public class Test{
    public static void main(String[] argv){
        String s1="abc";
        String s2=(String) s1.clone();
    }    
}

为什么这个简单的测试程序不起作用?

import java.util.*;
import java.lang.*;

public class Test{
    public static void main(String[] argv){
        String s1="abc";
        String s2=(String) s1.clone();
    }    
}

Why this simple test program doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

霊感 2025-01-09 22:52:21

clone是Object类的一个方法。对于“可克隆”的类,它应该实现标记 Cloneable 接口。 String 类没有实现此接口,也没有重写克隆方法,因此会出现错误。

我希望上面的代码片段用于教育目的,因为您永远不会觉得需要对 Java 中的字符串调用 clone ,因为:

  1. Java 中的字符串是不可变的。随意在方法/类之间共享它们
  2. 已经存在一个构造函数 new String(String) ,它的作用类似于复制构造函数,几乎相当于您的 clone() 调用。

clone is a method of the Object class. For a class to be "cloneable" it should implement the marker Cloneable interface. String class doesn't implement this interface and doesn't override the clone method hence the error.

I hope the above snippet is for educational purposes because you should never feel a need to call clone on strings in Java given that:

  1. Strings in Java are immutable. Feel free to share them across methods/classes
  2. There already exists a constructor new String(String) which acts like a copy constructor and is pretty much equivalent to your clone() call.
深海里的那抹蓝 2025-01-09 22:52:21

Object.clone() 受到保护。这是一个使用起来很棘手的 API。

通常,当通过扩大方法的可见性来扩展 Object 时,就会公开 clone()

任何字符串上的克隆都没有什么意义,因为它既是最终的又是不可变的。

复制字符串是有原因的;可以通过以下方式完成:

String s1 = ...;
String s2 = new String(s1)

Object.clone() is protected. It is a tricky API to use.

Usually one exposes clone() when one extends Object by broadening the method's visibility.

Clone on any string has little meaning, since it is both final and immutable.

There is a reason to copy a string; that can be done with:

String s1 = ...;
String s2 = new String(s1)
各空 2025-01-09 22:52:21

clone() is a protected method on the Object class. If you want a class to be cloneable the general pattern is to implement Cloneable and make that method public.

惜醉颜 2025-01-09 22:52:21

显然无法编译。 Object.clone 具有受保护的访问权限。

除了可以在类本身内访问以及在
同一个包...,受保护的成员也可以从
类通过对象引用至少具有相同的类型
班级

It obviously couldn't be compiled. Object.clone has protected access.

Beyond being accessible within the class itself and to code within the
same package..., a protected member can also be accessed from a
class through object references that are of at least the same type as
the class

酒与心事 2025-01-09 22:52:21

对于“可克隆”的类,它应该实现标记 Cloneable 接口。 String 类没有实现此接口,也没有重写克隆方法,因此会出现错误。

protected Object clone() 抛出CloneNotSupportedException 创建并返回该对象的精确副本(克隆)。

Java 中的字符串是不可变的。请随意在方法/类之间共享它们
已经存在一个构造函数 new String(String) ,它的作用类似于复制构造函数,几乎等同于您的 clone() 调用。

通常,当通过扩大方法的可见性来扩展 Object 时,就会公开 clone() 。

任何字符串上的克隆都没有什么意义,因为它既是最终的又是不可变的。

For a class to be "cloneable" it should implement the marker Cloneable interface. String class doesn't implement this interface and doesn't override the clone method hence the error.

protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object.

Strings in Java are immutable. Feel free to share them across methods/classes
There already exists a constructor new String(String) which acts like a copy constructor and is pretty much equivalent to your clone() call.

Usually one exposes clone() when one extends Object by broadening the method's visibility.

Clone on any string has little meaning, since it is both final and immutable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文