簡體   English   中英

在 Java 中,super.clone() 方法如何“知道”哪個 object 調用了它?

[英]In Java, how super.clone() method “knows” which object has called it?

我真的對此感到困惑(也許這對你來說聽起來很奇怪,對此感到抱歉)。 For example, if I have class A and override clone method and in body I have super.clone().. It calls clone() method from Object class, and my question is HOW clone method in Object class knows which object to clone.可能我不是很懂超(我知道用這個的時候就知道用當前的object之類的)。

所有 Java 類,如果它們沒有定義明確的extends ,則擴展Object super.clone() super.someMethod()任何調用都將調用 class 層次結構中的一個鏈接的該方法的實現,而不是調用 class 中定義的該方法的實現。

舉個例子

public class A {
    public void someMethod(){
        System.out.println("A.someMethod()");
    }
}

public class B extens A {
    @Override
    public void someMethod(){
        super.someMethod(); //call the implementation of someMethod() as defined in A
        System.out.println("B.someMethod()");
    }
}

這個:

new A().someMethod();

將打印A.someMethod();

和這個:

new B().someMethod();

將打印

A.someMethod()
B.someMethod()

編輯 1

一個稍微復雜一點的例子:

public class SuperTest {

    public static class A {

        public void someMethod() {
            System.out.println(
                    this.getClass().getSimpleName()+".someMethod()");
        }
    }

    public static class B extends A {

        @Override
        public void someMethod() {
            super.someMethod();
        }
    }

    public static void main(String[] args) {
        new B().someMethod();
    }
}

將打印B.someMethod()

super用於調用 class 層次結構更高的實現,但它仍然在執行super調用的 object 上調用。


編輯 2

因為這與clone()相關,它本身並不是很好,應該不惜一切代價避免。 您可以閱讀整個互聯網的原因。

clone()的規范實現確實需要 Class 的 class 層次結構中的所有類來提供clone()的規范實現。 clone()的實現應該從調用super.clone()開始,因為我們希望父 class 將克隆該類的所有成員並分配它們並返回 object。 然后子類的clone()實現應該復制其成員字段並將它們分配給克隆。

我們可以在擴展示例中看到這種情況

public class SuperCloneTest {

    public static class A implements Cloneable {

        private String member1;

        public A(String value) {
            this.member1 = value;
        }

        @Override
        public Object clone() {
            System.out.println("In A.clone()");
            System.out.printf("Class of `this` in A.clone(): %s\n", this.getClass().getSimpleName());
            A clone;
            try {
                clone = (A) super.clone();

                System.out.printf("Class of clone in A.clone(): %s\n", clone.getClass().getSimpleName());
                System.out.printf("Value of member1 in A.clone(): %s\n", this.member1);

                clone.member1 = this.member1;

                return clone;
            } catch (CloneNotSupportedException e) {
                //A implements Cloneable so we can be sure this won't happen
                throw new UnsupportedOperationException(e);
            }
        }
    }

    public static class B extends A {

        private String member2;

        public B(String value1, String value2) {
            super(value1);
            this.member2 = value2;
        }

        @Override
        public Object clone() {
            System.out.println("In B.clone()");
            System.out.printf("Class of `this` in B.clone(): %s\n", this.getClass().getSimpleName());

            B clone = (B) super.clone();
            System.out.printf("Class of clone in B.clone(): %s\n", clone.getClass().getSimpleName());
            System.out.printf("Value of member2 in B.clone(): %s\n", this.member2);

            clone.member2 = this.member2;

            return clone;
        }
    }

    public static class C extends B {

        private String member3;

        public C(String value1, String value2, String value3) {
            super(value1, value2);
            this.member3 = value3;
        }

        @Override
        public Object clone() {
            System.out.println("In C.clone()");
            System.out.printf("Class of `this` in C.clone(): %s\n", this.getClass().getSimpleName());

            C clone = (C) super.clone();
            System.out.printf("Class of clone in C.clone(): %s\n", clone.getClass().getSimpleName());
            System.out.printf("Value of member3 in C.clone(): %s\n", this.member3);

            clone.member3 = this.member3;

            return clone;
        }
    }

    public static void main(String[] args) {

        new C("value1", "value2", "value3").clone();
    }
}

當我們運行它時,我們得到:

In C.clone()
Class of `this` in C.clone(): C
In B.clone()
Class of `this` in B.clone(): C
In A.clone()
Class of `this` in A.clone(): C
Class of clone in A.clone(): C
Value of member1 in A.clone(): value1
Class of clone in B.clone(): C
Value of member2 in B.clone(): value2
Class of clone in C.clone(): C
Value of member3 in C.clone(): value3

在這里我們可以看到this總是 C 和克隆。 一直向上堆棧並在A中調用 super.clone( super.clone()調用Object.cone() ,它將分配與this相同類型的 object 。

Object 中的克隆方法是默認實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM