java 继承中的 clone() 方法调用委托
Delegation of clone() method call in inheritance in java
1.Child class 扩展父级 class。
2.Child class 实现 Cloneable 并覆盖 clone() 方法调用 super.clone()
3.Parent class 没有实现 Cloneable 接口,也没有覆盖 clone() 方法。
输出:
父和子 class 状态都被克隆。
问题:
当 Parent class 没有实现标记接口 Cloneable 时,Object class 如何克隆 Parent class 状态?
class ParentCloneableClass{
String val;
public ParentCloneableClass(String val){
this.val = val;
}
public String getVal() {
return val;
}
}
class CloneableClass extends ParentCloneableClass implements Cloneable{
String name;
public CloneableClass(String name){
super("parentClass");
this.name = name;
}
@Override
public CloneableClass clone() throws CloneNotSupportedException {
return (CloneableClass) super.clone();
}
public String getName() {
return name;
}
}
class Demo{
public static void main(String[] args) throws CloneNotSupportedException {
CloneableClass cloneableClass = new CloneableClass("deepak");
CloneableClass cloneableClassCloned = cloneableClass.clone();
}
}
class Object
检查 runtime type of the object being cloned, not the compile time type. So the object at runtime is of type Cloneable
which does implements Cloneable
, hence the Object
class performs the cloning accordingly:
The method clone for class Object
performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable
, then a CloneNotSupportedException
is thrown... Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
1.Child class 扩展父级 class。 2.Child class 实现 Cloneable 并覆盖 clone() 方法调用 super.clone() 3.Parent class 没有实现 Cloneable 接口,也没有覆盖 clone() 方法。
输出: 父和子 class 状态都被克隆。
问题: 当 Parent class 没有实现标记接口 Cloneable 时,Object class 如何克隆 Parent class 状态?
class ParentCloneableClass{
String val;
public ParentCloneableClass(String val){
this.val = val;
}
public String getVal() {
return val;
}
}
class CloneableClass extends ParentCloneableClass implements Cloneable{
String name;
public CloneableClass(String name){
super("parentClass");
this.name = name;
}
@Override
public CloneableClass clone() throws CloneNotSupportedException {
return (CloneableClass) super.clone();
}
public String getName() {
return name;
}
}
class Demo{
public static void main(String[] args) throws CloneNotSupportedException {
CloneableClass cloneableClass = new CloneableClass("deepak");
CloneableClass cloneableClassCloned = cloneableClass.clone();
}
}
class Object
检查 runtime type of the object being cloned, not the compile time type. So the object at runtime is of type Cloneable
which does implements Cloneable
, hence the Object
class performs the cloning accordingly:
The method clone for class
Object
performs a specific cloning operation. First, if the class of this object does not implement the interfaceCloneable
, then aCloneNotSupportedException
is thrown... Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.