為了解決上篇中 RationalTrait 的問題,有兩個解決方案,第一方案是使用預先初始化成員的值的方法,這種方法可以讓你在調用父類構造函數(shù)之前首先初始化子類的成員。 這種方法,是把初始化成員變量的定義放在調用父構造函數(shù)之前。
下面是一個匿名實例化 Trait 的例子
val x = 1
new {
val numerArg =1 * x
val denomArg = 2 *x
} with RationalTrait
res1: RationalTrait = 1/2
可以看到在這個例子中,我們把初始化成員的代碼放在其父 Trait 之前。 再看一個例子:
object twoThirds extends {
val numerArg =1 * x
val denomArg = 2 *x
} with RationalTrait
defined object twoThirds
初始化成員部分也是防止其父 Trait 之前,兩個方法都使用 with。
這種方法除了可以應用中匿名實例和對象外,也可以應用中類的定義說,比如我們可以定義如下的 RationalClass
class RationalClass(n:Int,d:Int) extends {
val numerArg =n
val denomArg =d
} with RationalTrait {
def + (that: RationalClass) = new RationalClass(
numer * that.denom + that.numer * denom,
denom * that.denom
)
}
因為這些預先初始化的值發(fā)生在調用父類的構造函數(shù)之前,因此這些初始化這些值時不可以引用正在構造的對象,正因為如此,如果在初始化的表達式中使用 this,這個 this 不是指正在構造的對象,而是指包含這個定義的對象。比如:
new {
val numberArg =1
val denomArg = this.numerArg *2
} with RationalTrait
<console>:11: error: value numerArg is not a member of object $iw
val denomArg = this.numerArg *2
這個例子無法編譯,這是因為編譯器無法找到 對象 $iw 的 numerArg 成員,$iw 為 Scala 命令行輸入的當前對象。