什么时候克隆可能不是原始对象的类型?
When might a clone not be the type of the original object?
我有一个 class 存储某些事件并给出克隆,但是 Eclipse(我的 IDE)给我未经检查的克隆警告:
import java.util.*;
import java.awt.event.*;
public PlayerEvents {
private final ArrayList<KeyEvent> keyevents;
private final ArrayList<MouseEvent> mouseevents;
@SuppressWarnings("unchecked") // I have to do this to keep the IDE quiet
public PlayerEvents(ArrayList<KeyEvent> keyevents, ArrayList<MouseEvent> mouseevents) {
this.keyevents = (ArrayList<KeyEvent>) keyevents.clone(); // Type safety: Unchecked cast from Object to ArrayList<KeyEvent>
this.mouseevents = (ArrayList<MouseEvent>) mouseevents.clone(); // Type safety: Unchecked cast from Object to ArrayList<KeyEvent>
}
// More code...
}
什么时候克隆的对象不是是它的源,即什么时候克隆的keyevent
不是keyevent
的类型?
编辑: 此外,为什么 .clone() 对象给出一个对象?这与 Java 语言的限制有关吗?
我在 Java 1.8.0_91.
中使用 Eclipse Mars (4.5.2)
ArrayList
return类型Object
。它将始终与 ArrayList
的类型相同,但您仍然需要强制转换。这是一个语法问题。
C.f。 ArrayList.clone()
关于你的第二个问题:是的,最初在 Java 方法中 return 值不能是协变的。您必须 return 确切的类型作为被覆盖的方法。因此,Object.clone()
只能 return 键入 Object
,这也是它的重写方法也必须做的。
发生这种情况是因为在运行时,由于 type ereasure.
,JVM 无法确保克隆方法 () 将 return 变成 ArrayList<MouseEvent>
关于clone方法,必须注意以下几点:
By default, java cloning is ‘field by field copy’ i.e. as the Object
class does not have idea about the structure of class on which clone()
method will be invoked. So, JVM when called for cloning, do following
things:
1) If the class has only primitive data type members then a completely
new copy of the object will be created and the reference to the new
object copy will be returned.
2) If the class contains members of any class type then only the
object references to those members are copied and hence the member
references in both the original object as well as the cloned object
refer to the same object.
Here's the best clone explanation I've ever found. Quick and direct.
我有一个 class 存储某些事件并给出克隆,但是 Eclipse(我的 IDE)给我未经检查的克隆警告:
import java.util.*;
import java.awt.event.*;
public PlayerEvents {
private final ArrayList<KeyEvent> keyevents;
private final ArrayList<MouseEvent> mouseevents;
@SuppressWarnings("unchecked") // I have to do this to keep the IDE quiet
public PlayerEvents(ArrayList<KeyEvent> keyevents, ArrayList<MouseEvent> mouseevents) {
this.keyevents = (ArrayList<KeyEvent>) keyevents.clone(); // Type safety: Unchecked cast from Object to ArrayList<KeyEvent>
this.mouseevents = (ArrayList<MouseEvent>) mouseevents.clone(); // Type safety: Unchecked cast from Object to ArrayList<KeyEvent>
}
// More code...
}
什么时候克隆的对象不是是它的源,即什么时候克隆的keyevent
不是keyevent
的类型?
编辑: 此外,为什么 .clone() 对象给出一个对象?这与 Java 语言的限制有关吗?
我在 Java 1.8.0_91.
中使用 Eclipse Mars (4.5.2)ArrayList
return类型Object
。它将始终与 ArrayList
的类型相同,但您仍然需要强制转换。这是一个语法问题。
C.f。 ArrayList.clone()
关于你的第二个问题:是的,最初在 Java 方法中 return 值不能是协变的。您必须 return 确切的类型作为被覆盖的方法。因此,Object.clone()
只能 return 键入 Object
,这也是它的重写方法也必须做的。
发生这种情况是因为在运行时,由于 type ereasure.
,JVM 无法确保克隆方法 () 将 return 变成ArrayList<MouseEvent>
关于clone方法,必须注意以下几点:
By default, java cloning is ‘field by field copy’ i.e. as the Object class does not have idea about the structure of class on which clone() method will be invoked. So, JVM when called for cloning, do following things:
1) If the class has only primitive data type members then a completely new copy of the object will be created and the reference to the new object copy will be returned.
2) If the class contains members of any class type then only the object references to those members are copied and hence the member references in both the original object as well as the cloned object refer to the same object.
Here's the best clone explanation I've ever found. Quick and direct.