简体   繁体   English

可以在子类的对象上调用受保护的方法而不会覆盖它们。 为什么java.lang.Object中的clone方法不同?

[英]Protected methods can be called on the object of subclass without overriding. Why it's different for clone method in java.lang.Object?

If I have a class A 如果我有A班

    public class A {
        protected void show() {
        }
    }

Another class B 另一类B

    public class B extends A {
    }

Driver class: 驱动类别:

    public class Driver {
          public static void main(String[] args) {
                  B b = new B();
                  b.show();
          }
    }

This thing works fine. 这个东西工作正常。 But why is it different for clone method in Object class? 但是,为什么Object类中的克隆方法有所不同? If I don't override the clone method with a public modifier I get an error like: "clone has protected access in Object". 如果我不使用公共修饰符覆盖clone方法,则会收到类似以下错误:“ clone在对象中具有受保护的访问”。 We know Object is by default parent of all classes. 我们知道Object默认是所有类的父级。 So, this should also work the same as for the classes Driver, A and B. Why is it different? 因此,这也应与Driver,A和B类的工作原理相同。为什么会有区别?

From javadoc javadoc

the clone() method in a class must respect some conventions : 类中的clone()方法必须遵守一些约定:

By convention, the returned object should be obtained by calling super.clone. 按照约定,应该通过调用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(). 如果一个类及其所有超类(对象除外)都遵守此约定,则x.clone()。getClass()== x.getClass()就是这种情况。

By convention, the object returned by this method should be independent of this object (which is being cloned). 按照惯例,此方法返回的对象应独立于该对象(正在克隆)。 To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. 为了实现这种独立性,可能有必要在返回super.clone之前修改该对象的一个​​或多个字段。 Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. 通常,这意味着复制构成要克隆对象的内部“深度结构”的任何可变对象,并用对副本的引用替换对这些对象的引用。 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. 如果一个类仅包含基本字段或对不可变对象的引用,则通常情况是无需修改super.clone返回的对象中的任何字段。

The method clone for class Object performs a specific cloning operation. 类Object的方法clone执行特定的克隆操作。 First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. 首先,如果该对象的类未实现Cloneable接口,则抛出CloneNotSupportedException。 Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. 注意,所有数组都被认为实现了Cloneable接口,并且数组类型T []的clone方法的返回类型为T [],其中T是任何引用或原始类型。 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. 因此,此方法执行此对象的“浅复制”,而不是“深复制”操作。

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. Object类本身并不实现Cloneable接口,因此在其类为Object的对象上调用clone方法将导致在运行时引发异常。

It means that either you have to override the clone() method and implements Cloneable OR use clone() directly with handling of a thrown CloneNotSupportedException 这意味着您要么必须重写clone()方法并实现Cloneable ,要么直接使用clone()处理抛出的CloneNotSupportedException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 java.lang.Object 中保护了 clone() 方法? - Why is the clone() method protected in java.lang.Object? 为什么java.lang.Object中的finalize()方法受到“保护”? - Why is the finalize() method in java.lang.Object “protected”? 实现克隆方法 - 错误 java:clone() 在 java.lang.Object 中具有受保护的访问权限 - Implementing clone method - Error java: clone() has protected access in java.lang.Object java.lang.Object 的受保护方法如何保护子类? - How are java.lang.Object's protected methods protected from subclasses? 在java.lang.Object类中拥有受保护方法的目的是什么? - What is the purpose of having protected methods in java.lang.Object class? 我什么时候应该重写 java.lang.Object 方法? - When should I be overriding java.lang.Object methods? 为什么java.lang.Cloneable不会覆盖java.lang.Object中的clone()方法? - Why does java.lang.Cloneable not override the clone() method in java.lang.Object? 如何修复proguard警告'找不到引用的方法'用于类java.lang.Object的现有方法'clone'和'finalize' - how to fix proguard warning 'can't find referenced method' for existing methods 'clone' and 'finalize' of class java.lang.Object 为什么不能克隆java.lang.Object? - Why java.lang.Object can not be cloned? 为什么 java.lang.Object class clone() 方法没有主体? - Why doesn't the java.lang.Object class clone() method have a body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM