[英]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.