java中的clone()是浅拷贝吗?

发布于 2024-10-21 17:01:48 字数 334 浏览 1 评论 0原文

java中的clone()是浅拷贝吗?

最终到达克隆() Object 的方法(最上面的 类),它创建一个新实例 与对象属于同一类,并且 将所有字段复制到新字段 实例(“浅拷贝”)。

我从维基百科读到了这篇文章。

我不明白为什么它是浅拷贝。 clone() 将创建一个包含所有字段的新实例。这只是深拷贝吗?使困惑。需要给我一些解释。

Is clone() in java a shallow copy?

Eventually this gets to the clone()
method of Object (the uppermost
class), which creates a new instance
of the same class as the object and
copies all the fields to the new
instance (a "shallow copy").

I read this from wikipedia.

I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me.

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

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

发布评论

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

评论(8

红墙和绿瓦 2024-10-28 17:01:48

默认的Object.clone()确实是一个浅拷贝。但是,除非您的对象实现了 Cloneable,否则它会抛出 CloneNotSupportedException

当您实现Cloneable时,您应该重写clone(),通过调用clone()< /code> 所有本身可克隆的字段。

The default Object.clone() is indeed a shallow copy. However, it's designed to throw a CloneNotSupportedException unless your object implements Cloneable.

And when you implement Cloneable, you should override clone() to make it do a deep copy, by calling clone() on all fields that are themselves cloneable.

月棠 2024-10-28 17:01:48

它是浅复制,因为它只复制对其他对象的引用。假设我们有这些类:

class A {
    B variable
    A() {
        variable = new B();
    }
}

class B { }

现在我们克隆 A 的实例:

A firstA = new A();
A secondA = firstA.clone();

firstA 和 secondaryA 中的 B 实例将是相同的。您不会拥有 B 实例的副本。这就是为什么clone()被称为浅拷贝。

您链接的页面上的图表应该可以帮助您理解所有这些。

It is a shallow copy because it only copies reference to other objects. Say we have these classes :

class A {
    B variable
    A() {
        variable = new B();
    }
}

class B { }

And now we make a clone of an instance of A :

A firstA = new A();
A secondA = firstA.clone();

The B instance in firstA and secondA will be the same. You won't have a copy of the B instance. This is why clone() is said to do shallow copy.

The diagrams on the page you linked should help you understand all that.

闻呓 2024-10-28 17:01:48

顺便说一句,我很惊讶没有人提到Joshua Bloch 对 Cloneable 的看法

如果您已阅读有关克隆的文章
在我的书中,特别是如果你读过
字里行间,你就会知道
我认为克隆已经彻底崩溃了。那里
有一些设计缺陷,其中最大的
这就是 Cloneable 接口
没有克隆方法。还有那个
意味着它根本不起作用:
Cloneable 没有说的东西
任何关于你可以做什么的事情
它。相反,它说了一些关于
它在内部可以做什么。它说
如果通过调用 super.clone
反复调用 Object 的
克隆方法,该方法将返回
原件的现场副本。

As an aside, I'm surprised nobody has mentioned Joshua Bloch's views on Cloneable

If you've read the item about cloning
in my book, especially if you read
between the lines, you will know that
I think clone is deeply broken. There
are a few design flaws, the biggest of
which is that the Cloneable interface
does not have a clone method. And that
means it simply doesn't work: making
something Cloneable doesn't say
anything about what you can do with
it. Instead, it says something about
what it can do internally. It says
that if by calling super.clone
repeatedly it ends up calling Object's
clone method, this method will return
a field copy of the original.

遥远的她 2024-10-28 17:01:48

clone() 创建所有字段的副本。
Java 具有原始类型和引用 - 当您克隆对象时,您会得到一个新对象,其中包含所有原始字段的副本(就像深层复制),而且您还拥有所有引用字段的副本。因此,结果你得到两个对象,它们拥有基元的副本和对相同对象的引用的副本 - 原始对象和复制的对象都将使用相同的对象。

clone() creates copy of all fields.
Java have primitive types and refences - when you clone your object you get a new object with copies of all primitive field (it is like deep copy) but also you have copy of all refernce fields. So in result you get two objects with they own copies of primitives and copies of references to the same objects - both original and copied object will use the same objects.

微暖i 2024-10-28 17:01:48

有些对象不提供深层复制。例如,ArrayList 将克隆列表,但不会克隆列表中的元素。以下内容来自 JavaDoc< /a> 对于 ArrayList:

公共对象克隆()

    返回此 ArrayList 实例的浅表副本。 (元素本身不会被复制。)

Some objects do not provide a deep copy. For example, an ArrayList will clone the list, but not the elements in the list. The following is from the JavaDoc for ArrayList:

public Object clone()

    Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
春夜浅 2024-10-28 17:01:48

Object.clone() 的默认实现是浅拷贝。此行为对于具有大量原始字段或不可变字段的类型仍然有用。您可以查看如何正确重写克隆方法?了解如何正确重写它。调用 super.clone() 并转换生成的对象后,您可以根据需要克隆得更深。

隐含地,随着类型中复杂、可变字段数量的增加,克隆的价值就会减少。

The default implementation of Object.clone() is a shallow copy. This behavior is still useful for types that have a large number of primitive fields or Immutable fields. You can look at How to properly override clone method? for how to properly override it. After calling super.clone(), then casting the resulting object, you can then clone deeper as needed.

Implicitly, the value of clone diminishes as the number of complex, mutable fields on your type increases.

枕花眠 2024-10-28 17:01:48

clone 的作用是为每个选择支持克隆的对象定义的。 Object.clone 是受保护的,因此任何对象都不允许克隆,除非有人专门定义了它。

What clone does is defined for each object that chooses to support clone. Object.clone is protected, so no object allows clone unless someone has specifically defined it.

樱&纷飞 2024-10-28 17:01:48

是的。

但首先你需要你的类实现 Cloneable 并抛出异常

class A implements Cloneable{
    public int y;
    public B b;

    public A(){
        b = new B();
    }

    public static void main(String[] args) throws CloneNotSupportedException{
        A a = new A();
        A a2 = (A) a.clone();
        System.out.print(a.b==a2.b);
    }
}

输出: true

Yes.

But first you need that your class will implement Cloneable and throw exception

class A implements Cloneable{
    public int y;
    public B b;

    public A(){
        b = new B();
    }

    public static void main(String[] args) throws CloneNotSupportedException{
        A a = new A();
        A a2 = (A) a.clone();
        System.out.print(a.b==a2.b);
    }
}

Output: true

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