“clone”方法是否会克隆被覆盖的方法?

huangapple go评论101阅读模式
英文:

Does the clone method clone overridden methods?

问题

以下是翻译好的部分:

如果我克隆下面类的一个实例,并且在实例化时重写一个方法,那么克隆会有被重写的方法吗?我在以下链接中没有找到有关这种行为的任何信息:

  1. public class ToBeCloned implements Cloneable{
  2. public int returnInt() {
  3. return 1;
  4. }
  5. public void printTest() {
  6. System.out.println("returnInt():" + returnInt() + "\nToBeCloned 原始");
  7. }
  8. @Override
  9. public ToBeCloned clone() throws CloneNotSupportedException {
  10. return (ToBeCloned) super.clone();
  11. }
  12. }

链接1:https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html
链接2:https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()

英文:

If I clone an instance of the following class, and overridde a method when instancing, will the clone have the overridden method? I haven't found anything regarding this behavior in
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()
.

  1. public class ToBeCloned implements Cloneable{
  2. public int returnInt() {
  3. return 1;
  4. }
  5. public void printTest() {
  6. System.out.println("returnInt():"+returnInt()+"\nToBeCloned Original");
  7. }
  8. @Override
  9. public ToBeCloned clone() throws CloneNotSupportedException {
  10. return (ToBeCloned) super.clone();
  11. }
  12. }

答案1

得分: 6

如果你做类似这样的事情:

  1. new ToBeCloned() { @Override...}

这只是一种创建子类并实例化的简便方式。如果你克隆那个实例,你会得到另一个相同匿名子类的实例,拥有所有相同的方法。

英文:

If you do something like

  1. new ToBeCloned() { @Override...}

it is just a short way of creating a subclass and instantiating it. If you clone that instance, you get another instance of the same anonymous subclass, with all the same methods.

答案2

得分: 3

答案是肯定的,克隆体中将包含在JavaSE 1.8中至少被覆盖的方法。

这通过以下程序和其输出进行了说明:

  1. public class OverridingMethods {
  2. public static void main(final String[] args) {
  3. final ToBeCloned toBeCloned1 = new ToBeCloned();
  4. final ToBeCloned toBeCloned2 = new ToBeCloned() {
  5. @Override
  6. public int returnInt() {
  7. return 2;
  8. }
  9. @Override
  10. public void printTest() {
  11. System.out.println("returnInt():" + returnInt() + "\nToBeCloned Overridden");
  12. }
  13. };
  14. ToBeCloned toBeCloned3 = null;
  15. ToBeCloned toBeCloned4 = null;
  16. ToBeCloned toBeCloned5 = null;
  17. try {
  18. toBeCloned3 = toBeCloned1.clone();
  19. toBeCloned4 = toBeCloned2.clone();
  20. toBeCloned5 = toBeCloned4.clone();
  21. } catch (final CloneNotSupportedException e) {
  22. e.printStackTrace();
  23. }
  24. toBeCloned1.printTest();
  25. toBeCloned2.printTest();
  26. toBeCloned3.printTest();
  27. toBeCloned4.printTest();
  28. toBeCloned5.printTest();
  29. }
  30. }

程序的输出如下:

  1. returnInt():1
  2. ToBeCloned Original
  3. returnInt():2
  4. ToBeCloned Overridden
  5. returnInt():1
  6. ToBeCloned Original
  7. returnInt():2
  8. ToBeCloned Overridden
  9. returnInt():2
  10. ToBeCloned Overridden

这证明了覆盖的方法会被保留,即使是在已经克隆的实例中进行克隆。

英文:

The answer is yes, the clone will contain the overridden methods atleast in javaSE-1.8.

This is illustrated by the following programm and it's output:

  1. public class OverridingMethods {
  2. public static void main(final String[] args) {
  3. final ToBeCloned toBeCloned1 = new ToBeCloned();
  4. final ToBeCloned toBeCloned2 = new ToBeCloned() {
  5. @Override
  6. public int returnInt() {
  7. return 2;
  8. }
  9. @Override
  10. public void printTest() {
  11. System.out.println("returnInt():"+returnInt()+"\nToBeCloned Overridden");
  12. }
  13. };
  14. ToBeCloned toBeCloned3 = null;
  15. ToBeCloned toBeCloned4 = null;
  16. ToBeCloned toBeCloned5 = null;
  17. try {
  18. toBeCloned3 = toBeCloned1.clone();
  19. toBeCloned4 = toBeCloned2.clone();
  20. toBeCloned5 = toBeCloned4.clone();
  21. } catch (final CloneNotSupportedException e) {
  22. e.printStackTrace();
  23. }
  24. toBeCloned1.printTest();
  25. toBeCloned2.printTest();
  26. toBeCloned3.printTest();
  27. toBeCloned4.printTest();
  28. toBeCloned5.printTest();
  29. }
  30. }

The output of the programm is the following:

  1. returnInt():1
  2. ToBeCloned Original
  3. returnInt():2
  4. ToBeCloned Overridden
  5. returnInt():1
  6. ToBeCloned Original
  7. returnInt():2
  8. ToBeCloned Overridden
  9. returnInt():2
  10. ToBeCloned Overridden

This proofs that the overridden method is kept, even if cloning already cloned instances.

huangapple
  • 本文由 发表于 2020年8月25日 02:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63566554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定