克隆方法是否克隆覆盖的方法?
Does the clone method clone overridden methods?
如果我克隆了下面class的一个实例,并在实例化时重写了一个方法,那么克隆会有被重写的方法吗?我没有发现任何关于这种行为的信息
https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html nor https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()
.
public class ToBeCloned implements Cloneable{
public int returnInt() {
return 1;
}
public void printTest() {
System.out.println("returnInt():"+returnInt()+"\nToBeCloned Original");
}
@Override
public ToBeCloned clone() throws CloneNotSupportedException {
return (ToBeCloned) super.clone();
}
}
答案是肯定的,克隆将至少包含 javaSE-1.8 中的重写方法。
下面的程序及其输出说明了这一点:
public class OverridingMethods {
public static void main(final String[] args) {
final ToBeCloned toBeCloned1 = new ToBeCloned();
final ToBeCloned toBeCloned2 = new ToBeCloned() {
@Override
public int returnInt() {
return 2;
}
@Override
public void printTest() {
System.out.println("returnInt():"+returnInt()+"\nToBeCloned Overridden");
}
};
ToBeCloned toBeCloned3 = null;
ToBeCloned toBeCloned4 = null;
ToBeCloned toBeCloned5 = null;
try {
toBeCloned3 = toBeCloned1.clone();
toBeCloned4 = toBeCloned2.clone();
toBeCloned5 = toBeCloned4.clone();
} catch (final CloneNotSupportedException e) {
e.printStackTrace();
}
toBeCloned1.printTest();
toBeCloned2.printTest();
toBeCloned3.printTest();
toBeCloned4.printTest();
toBeCloned5.printTest();
}
}
程序的输出如下:
returnInt():1
ToBeCloned Original
returnInt():2
ToBeCloned Overridden
returnInt():1
ToBeCloned Original
returnInt():2
ToBeCloned Overridden
returnInt():2
ToBeCloned Overridden
这证明覆盖的方法被保留,即使克隆已经克隆的实例。
如果你这样做
new ToBeCloned() { @Override...}
这只是创建子类并实例化它的一种简短方法。如果您克隆该实例,您将获得同一个匿名子类的另一个实例,具有所有相同的方法。
如果我克隆了下面class的一个实例,并在实例化时重写了一个方法,那么克隆会有被重写的方法吗?我没有发现任何关于这种行为的信息 https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html nor https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone() .
public class ToBeCloned implements Cloneable{
public int returnInt() {
return 1;
}
public void printTest() {
System.out.println("returnInt():"+returnInt()+"\nToBeCloned Original");
}
@Override
public ToBeCloned clone() throws CloneNotSupportedException {
return (ToBeCloned) super.clone();
}
}
答案是肯定的,克隆将至少包含 javaSE-1.8 中的重写方法。
下面的程序及其输出说明了这一点:
public class OverridingMethods {
public static void main(final String[] args) {
final ToBeCloned toBeCloned1 = new ToBeCloned();
final ToBeCloned toBeCloned2 = new ToBeCloned() {
@Override
public int returnInt() {
return 2;
}
@Override
public void printTest() {
System.out.println("returnInt():"+returnInt()+"\nToBeCloned Overridden");
}
};
ToBeCloned toBeCloned3 = null;
ToBeCloned toBeCloned4 = null;
ToBeCloned toBeCloned5 = null;
try {
toBeCloned3 = toBeCloned1.clone();
toBeCloned4 = toBeCloned2.clone();
toBeCloned5 = toBeCloned4.clone();
} catch (final CloneNotSupportedException e) {
e.printStackTrace();
}
toBeCloned1.printTest();
toBeCloned2.printTest();
toBeCloned3.printTest();
toBeCloned4.printTest();
toBeCloned5.printTest();
}
}
程序的输出如下:
returnInt():1
ToBeCloned Original
returnInt():2
ToBeCloned Overridden
returnInt():1
ToBeCloned Original
returnInt():2
ToBeCloned Overridden
returnInt():2
ToBeCloned Overridden
这证明覆盖的方法被保留,即使克隆已经克隆的实例。
如果你这样做
new ToBeCloned() { @Override...}
这只是创建子类并实例化它的一种简短方法。如果您克隆该实例,您将获得同一个匿名子类的另一个实例,具有所有相同的方法。