這很有趣!如果你把類定義成一個文件,並使用scalac -print test.scala
編譯它,你會看到這樣的事情,
[[syntax trees at end of cleanup]] // scala
package <empty> {
class A extends Object {
def g(): String = A.f();
def <init>(): A = {
A.super.<init>();
()
}
};
object A extends Object {
private[this] val f: String = _;
<stable> <accessor> def f(): String = A.this.f;
def <init>(): A.type = {
A.super.<init>();
A.this.f = "Object A";
()
}
};
class B extends A {
<synthetic> private[this] val x$1: runtime.BoxedUnit = _;
def <init>(): B = {
B.super.<init>();
B.this.x$1 = {
case <synthetic> val x1: String = ("Object B": String("Object B"));
case5(){
if (A.f().==(x1))
{
val x2: String = x1;
matchEnd4(scala.runtime.BoxedUnit.UNIT)
}
else
case6()
};
case6(){
matchEnd4(throw new MatchError(x1))
};
matchEnd4(x: runtime.BoxedUnit){
scala.runtime.BoxedUnit.UNIT
}
};
()
}
}
}
這表明編譯器創建一個類B
與執行的匹配檢查,看看該值初始化您用於覆蓋val A.f
等於原始值if (A.f().==(x1))
。似乎不太有用的權利?如果它們不相等,則通過調用case6()
來引發匹配錯誤。我們可以通過將class B
的定義更改爲override val A.f = "Object A"
來進行確認。
class B extends A {
override val A.f = "Object A"
}
scala> val b = new B
b: B = [email protected]
那麼如何解決它呢?你可以做到這一點,
class B extends A {
override def g = "Object B"
}
scala> val b = new B
b: B = [email protected]
scala> b.g
res1: String = Object B
或
class B extends A {
val f = "Object B"
}
scala> val b = new B
b: B = [email protected]
scala> b.f
res0: String = Object B
感謝澄清!我同意你一般需要重寫companion對象,但是我想在這裏做的原因是爲了測試目的,我想用另一種方法替換伴隨對象中的方法。另外,一些語言允許重寫靜態方法。 – deepkimo 2013-04-30 21:05:37