super.clone()操作在派生类中不起作用 [英] super.clone() operation not works in Derived Class

查看:149
本文介绍了super.clone()操作在派生类中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之所以出现此问题,是因为我的项目中遇到了技术难题。

问题:
我需要克隆一个Class对象,该对象从第三方库类(我们无权修改其内容)扩展属性(继承)

Problem: I need to clone a Object of a Class where it extended the properties(Inheritance) from a third party library class(where we don't have access to modify its contents)

下面的示例让我解释一下:

Let me explain with example below:

父级:

public class UnChangeableBaseClass {

  //fields and Methods

}

子类:

class DerivedLocalClass extends UnChangeableBaseClass implements Cloneable {

   // local fields and methods


   public Object clone(){

      Object clonedObj= null;

      try{
       clonedObj = super.clone();
      }
      catch(CloneNotSupportedException e){
        //log exceptions
      }

   }

}

当我尝试这样做时, super.clone()该方法引用类- UnChangeableBaseClass 类型,它不会覆盖 Object clone()方法。我相信所有类都使用 java.lang.Object类进行了扩展,隐式受保护的对象clone()方法将被继承参加这个家长班。因此,我以这种方式认为派生类中的此方法将覆盖父/对象克隆方法。但是在运行时JVM中搜索 UnChangeableBaseClass 中明确定义的克隆方法。希望我能以正确的方式解释而不会引起您的困扰。

When I try to do this, super.clone() method refers to Class - UnChangeableBaseClass Type and it doesn't overrides the Object clone() methods. I believe all classes were extended with java.lang.Object class, implicitly protected Object clone() method would be inherited to this Parent Class. So, i thought in such a way that this method in Derived Class would overrides the Parent/Object clone method. But during runtime JVM search for the clone method explicitly defined in UnChangeableBaseClass. Hope I explained in proper way without confusing you.

我的问题如下:


  1. 在这种典型情况下如何实现克隆方法,在这种情况下我们无法在父类中添加任何方法

    来具有 super.clone ()调用对象克隆方法。

如果上述情况不可行,是否还有其他方法可以克隆派生类

对象(通过考虑上述场景中的所有限制)

If above case is not possible, is there any other way to clone the Derived Class
Object (by considering all the limitations in above scenario)

最后,仅了解这种JVM行为的原因(如上所述) )。

Finally, just to know the reason for this JVM behaviour (described above).


推荐答案

正确的方法签名在下面

@Override
public Object clone() throws CloneNotSupportedException {
    return super.clone();
}

克隆受保护 Object 类中的方法,以便您可以在类内部以及从中进行扩展。仅需要
super.clone()从调用方法<的对象中调用 clone()方法code> internalClone 放在 this 上,这是当前的类对象。

Clone is Protected method in Object class so it is accessible to you inside class, and if you are extending from it. super.clone() is just needed to call clone() method from object which calls method internalClone on this which is current class object.

   internalClone((Cloneable) this);

因此在 clone()方法上方,<如果调用对象的实例不是 Cloneable Object 仅会抛出 CloneNotSupportedException $ c>

So above clone() method inside Object will only throw CloneNotSupportedException if instance on which it is called is not Cloneable

我看到一些关于克隆方法的误解

I see some misconceptions about clone method


  1. clone()方法在 Object 类内受保护,因此您无法调用 clone()在课外。例如 child.clone()除非您覆盖它并使其成为公共访问权限

  2. Cloneable 是标记接口,如果您不标记类 Cloneable ,则将得到 CloneNotSupportedException 如果您调用 clone()方法

  3. 如果类仅包含基本字段或对不可变对象的引用,那么通常情况下,无需修改 super.clone 返回的对象中的字段。

  4. 按照惯例,返回的对象应通过调用 super.clone 获得。如果一个类及其所有超类(对象除外)都遵守此约定,则 x.clone()。getClass()会是这种情况。 == x.getClass()

  1. clone() method is protected inside Object class so you can not call clone() outside of class. e.g. child.clone() unless you override it and make access public
  2. Cloneable is marker interface and if you do not mark class Cloneable then you will get CloneNotSupportedException if you call clone() method
  3. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
  4. By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

因此下面的代码可以正常工作

So below code works fine

public  class Child extends UnChangeableBaseClass
        implements
            Cloneable {

    int index = 0;

    public Child(int index) {

        this.index = 10;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

参考文献:


  1. Object#clone()

  2. 可克隆

  1. Object#clone()
  2. Cloneable

这篇关于super.clone()操作在派生类中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆