简体   繁体   English

为什么 java.lang.Object class clone() 方法没有主体?

[英]Why doesn't the java.lang.Object class clone() method have a body?

There are lot's of questions on this platform regarding clone() method of Object class.这个平台上有很多关于 Object class 的 clone() 方法的问题。 Everyone is answering different.每个人的回答都不一样。 There are lots of question regarding why clone() is protected.关于为什么 clone() 受到保护有很多问题。 Some says..有人说..

  1. clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it. clone() 是受保护的,所以我们必须提供自己的 clone() 并从中间接调用 Object.clone() 。
  2. clone() is protected soo outside to java.lang package we can access this method in subclass directly or with subclass Object only. clone() 在 java.lang package 之外受到保护,我们可以直接在子类中访问此方法,也可以仅使用子类 Object 访问此方法。
  3. clone must not be called on non-cloneable objects, therefore it is not made public.不能在不可克隆的对象上调用 clone,因此它不会被公开。

Now Here are my questions.现在这是我的问题。

  1. If the above 2nd point is correct then how we can access clone() in subclass directly .如果上述第二点是正确的,那么我们如何直接访问子类中的 clone()
  2. What are non cloneable objects .什么是不可克隆对象
  3. What is the need to be Override clone() in subclass in case of cloning.在克隆的情况下需要在子类中覆盖克隆()。 Whereas the protected member of another package we can access inside another package by inheriting its parent class directly.而另一个 package 的受保护成员我们可以通过直接继承其父 class 来访问另一个 package 内部。
  4. Why Object.clone() dosen't have body part.为什么 Object.clone() 没有正文部分。

    protected native Object clone() throws CloneNotSupportedException;受保护的本机 Object clone() 抛出 CloneNotSupportedException;

If the above 2nd point is correct then how we can access clone() in subclass directly.如果上述第二点是正确的,那么我们如何直接访问子类中的 clone() 。

It is correct.它是正确的。

Like this:像这样:

public class Foo implements Cloneable {  
    public Foo copy() {
        return super.clone();  // or just `clone()`
    }
}

The point is that a method of a class can call protected methods in its superclass.关键是 class 的方法可以调用其超类中的protected方法。 Note that Foo needs to implement Cloneable if you want to enable the default implementation of clone() in java.lang.Object .请注意,如果要在java.lang.Object启用clone()的默认实现, Foo需要implement Cloneable But you don't have to enable it, and you don't have to use it even if it is enabled;但是你不必启用它,即使启用了也不必使用它; see below.见下文。

What are non cloneable objects.什么是不可克隆的对象。

They are objects that you can't call clone() on (successfully).它们是您不能(成功)调用clone()的对象。 The exact meaning depends on the context:具体含义取决于上下文:

  • It could be that objects that don't have a public clone method, and you cannot call the protected one.可能是没有public克隆方法的对象,您不能调用protected的对象。

  • It could be that the (non-public) clone method does not work;可能是(非公开)克隆方法不起作用; eg because the class doesn't implement Cloneable or extend one that does.例如,因为 class 没有implement Cloneable或扩展一个。

What is the need to override clone() in subclass in case of cloning.在克隆的情况下,需要在子类中覆盖clone() Whereas the protected member of another package we can access inside another package by inheriting its parent class directly.而另一个 package 的受保护成员我们可以通过直接继承其父 class 来访问另一个 package 内部。

There are three distinct reasons for overriding clone() :覆盖clone()有三个不同的原因:

  1. To make clone() accessible to other classes that wouldn't be able to access it otherwise.使其他无法访问它的类可以访问clone() That is, any classes that are NOT in the same package and do NOT extend this class.也就是说,任何不在同一个 package 中并且不扩展此 class 的类。

  2. To alter the behavior of the clone() method:要更改clone()方法的行为:

    • You don't have to call super.clone() .您不必调用super.clone() You can implement the cloning behavior anyway that you like.您可以随心所欲地实现克隆行为。
    • You could do things to the clone after calling super.clone() .你可以在调用super.clone()之后对克隆做一些事情。
  3. To override the return type so that you don't need to cast the result of clone() .覆盖返回类型,这样您就不需要转换clone()的结果。 (The return type of Object::clone is Object .) Object::clone的返回类型是Object 。)

Why Object.clone() doesn't have body part.为什么 Object.clone() 没有正文部分。

Did you notice the native modifier?你注意到native修饰符了吗? Native methods do not have a body.本机方法没有主体。

The cloning behavior of java.lang.Object is implemented in native code. java.lang.Object的克隆行为是在本机代码中实现的。 It is doing things that cannot be done in ordinary Java code.是做普通Java代码做不到的事情。 (Or using Java reflection, for that matter.) (或者使用 Java 反射,就此而言。)

 protected native Object clone() throws CloneNotSupportedException;

This is the prototype of clone method in Object class这是Object class中克隆方法的原型

why no Body?为什么没有身体?

It is a native method so it doesn't have the body cause it's body is provided by native library which is written in c language same as Jvm Or we can say we just need to give instruction to the jvm that we need to clone and the rest is taken care by Jvm它是一种本机方法,因此它没有主体,因为它的主体由本机库提供,该库是用 c 语言编写的,与 Jvm 相同,或者我们可以说我们只需要向 Z6CE80D8EF00F5E98FZEEEE941 的克隆提供指令即可rest 由 Jvm 负责

Why protected why not public?为什么保护为什么不公开?

Now clone() method is given as protected not public so that we couldn't clone Object class object现在 clone() 方法被指定为受保护的非公共方法,因此我们无法克隆 Object class object

This behaviour provided to the sub classes or the classes created by the users.And we know that protected members are accessible outside the package inside Subclass only and on subclass object only.这种行为提供给子类或用户创建的类。我们知道受保护的成员只能在子类内部的 package 之外访问,并且只能在子类 object 上访问。

why override?为什么要覆盖?

We override it in subclass because there is a possibility that we need to call clone method outside the class and because of protected Behaviour we won't be able to do that, so we override it with public modifier …prototype mentioned below我们在子类中覆盖它,因为有可能我们需要在 class 之外调用克隆方法,并且由于受保护的行为我们将无法做到这一点,所以我们用公共修饰符覆盖它......下面提到的原型

public Object clone() throws CloneNotSupportedException;

why object need to be cloneable?为什么 object 需要可克隆?

Since the cloning object is done by Jvm so there jvm need a information like on which object cloning is required and for that object is required to have cloneable signature, And for that we need to implement cloneable interface which is a marker interface and it will inform to jvm that this specific object need cloning Since the cloning object is done by Jvm so there jvm need a information like on which object cloning is required and for that object is required to have cloneable signature, And for that we need to implement cloneable interface which is a marker interface and it will inform到 jvm 这个特定的 object 需要克隆

暂无
暂无

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

相关问题 为什么在 java.lang.Object 中保护了 clone() 方法? - Why is the clone() method protected in java.lang.Object? 为什么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访问clone() - Accessing clone() from java.lang.Object 可以在子类的对象上调用受保护的方法而不会覆盖它们。 为什么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? 实现克隆方法 - 错误 java:clone() 在 java.lang.Object 中具有受保护的访问权限 - Implementing clone method - Error java: clone() has protected access in java.lang.Object 为什么java.lang.Object中的finalize()方法受到“保护”? - Why is the finalize() method in java.lang.Object “protected”? 为什么java.lang.Object不能转成T? - Why can java.lang.Object no be coverted into T? 找不到引用的方法'com.google.firebase.tasks.Task setValue(java.lang.Object,java.lang.Object)' - can't find referenced method 'com.google.firebase.tasks.Task setValue(java.lang.Object,java.lang.Object)' 通用类型获取方法在 foreach 循环中不起作用 - java:不兼容的类型:java.lang.Object 无法转换为错误 - Generic type get method doesn't work in a foreach loop - java: incompatible types: java.lang.Object cannot be converted to Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM