[英]Calling Super Method in Scala
我在specs2中編寫測試用例時遇到了這段代碼。
abstract class WithDbData extends WithApplication {
override def around[T: AsResult](t: => T): Result = super.around {
setupData()
t
}
def setupData() {
// setup data
}
}
"Computer model" should {
"be retrieved by id" in new WithDbData {
// your test code
}
"be retrieved by email" in new WithDbData {
// your test code
}
}
這是鏈接 。 請說明在這種情況下super.around
是如何工作的?
WithApplication
類中的around
方法具有以下簽名:
def around[T](t: ⇒ T)(implicit arg0: AsResult[T]): Result
讓我們忽略隱式參數,這種解釋沒有意思。
它使用一個按名字調用的參數 (t: ⇒ T)
WithDbData
(t: ⇒ T)
,在WithDbData
類的around
方法中,它使用一個塊進行調用,該塊是super.around
之后的{ ... }
。 該塊是作為參數t
傳遞的。
另一個簡單的示例來說明您可以執行以下操作:
// Just using the name 'block' inside the method calls the block
// (that's what 'call-by-name' means)
def repeat(n: Int)(block: => Unit) = for (i <- Range(0, n)) {
block // This calls the block
}
// Example usage
repeat(3) {
println("Hello World")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.