我發現一些容易混淆的使用特性的一些單元測試代碼,如:特徵實例化後的大括號如何工作?
trait MyTrait {
val t1 = ... //some expression
val t2 = ... //some expression
}
,然後使用新的,同時一些表達式被大括號包裹,隨後實例化實例化特質。
test("it is a test") {
new MyTrait {
// do something with t1 and t2
}
}
我很困惑這種奇怪的語法。
我的問題是:
爲什麼使用如下特質被大括號實例?
在這種情況下特徵實例化的目的是什麼以及其他情況也可能有幫助?
重複http://stackoverflow.com/questions/12891321/why-are-traits-instantiable, http://stackoverflow.com/questions/6155570/what-does-it-mean-to-instantiate-a-trait – sourcedelica 2013-04-29 14:56:43