美文网首页码字成文码农的世界
跟 JDK 学英语(2)- Cloneable

跟 JDK 学英语(2)- Cloneable

作者: 码语生活 | 来源:发表于2018-10-25 23:37 被阅读3次

一、原文与翻译

public interface Cloneable

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

一个类实现了Cloneable接口,以向 Object.clone() 方法表明,该方法对该类的实例进行字段对字段的复制是合法的。

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

在一个没有实现 Cloneable 接口的类实例上调用 Object 的 clone 方法会导致抛出 CloneNotSupportedException 异常。

By convention, classes that implement this interface should override Object.clone(which is protected) with a public method. See Object.clone() for details on overriding this method.

按照约定,实现了这个接口的类应该用一个公共方法覆写 Object.clone 方法,这个方法在 Object 类上是 protected 的。关于覆写这个方法详见官方文档。

Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

注意这个接口没有名为 clone 的方法。因此,不可能只是通过借助实现了这个接口来克隆一个对象。 即使 clone 方法被反射调用,也无法保证它会成功。
Since: JDK1.0
自 JDK1.0

二、词汇学习

implement: 实现,实施
indicate: 表明,象征,预示
convention: 约定,惯例,习俗
override: 覆写,推翻,比其他重要
contain: 包含
invoke: 调用,援引(法律、原则等)
by virtue of: 借助
reflectively: 反射地
guarantee: 保证

三、句子分析

A class implements the Cloneable interface to indicate to the Object.clone()method that it is legal for that method to make a field-for-field copy of instances of that class.

这是个长句,主干是 A class implements interface. 「to indicate to the method」表示目的:『向这个方法表明』。后面的 that 从句是要表明的内容:『字段对字段地复制这个类的实例是合法的』。

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

又一个长句,主干:Invoking method results in exception being thrown. 理解了主干后,其他修饰语往上加就好理解了。

最后一段用了较多的否定词,not contain, not possible, no guarantee,用于表达一种不确定性。

四、技术要点

Cloneable 接口用于表明实现了该接口的类是一个可克隆的类,但不保证能克隆成功。

通常,我们需要自己实现克隆的逻辑,一个字段是浅复制(只复制引用),还是深复制(复制值),取决于我们覆写的 clone 方法。

浅复制往往要考虑并发问题,或者是有多处入口改动了所引用的对象属性,就有可能引起一些莫名奇妙的问题。因此,写代码遇到调用 clone 方法的地方,要多留心。

微信公众号.jpg

相关文章

网友评论

    本文标题:跟 JDK 学英语(2)- Cloneable

    本文链接:https://www.haomeiwen.com/subject/ptdhtqtx.html