反编译Scala代码:为什么派生类中有两个被覆盖的方法?
class A
{
private var str: String = "A"
val x: A = this
override def toString(): String = str
def m1(other: AnyRef): AnyRef = {
println("This is A.m1(AnyRef)")
other
}
}
class B extends A {
private var str: String = "B"
var z: Int = 0
override val x: B = this
override def m1(other: AnyRef): B = {
println("This is B.m1(AnyRef)")
this
}
}
上面代码的class B被反编译为:
public class test$B extends test$A {
private java.lang.String str;
private int z;
private final test$B x;
private java.lang.String str();
private void str_$eq(java.lang.String);
public int z();
public void z_$eq(int);
public test$B x();
public test$B m1(java.lang.Object);
public java.lang.Object m1(java.lang.Object);
public test$A x();
public test$B();
}
我不明白为什么m1
反编译的代码中有两个“版本”的方法。据我了解,B.m1
只是覆盖A.m1
并public java.lang.Object m1(java.lang.Object)
属于A
并且不应该在 class 中B
。