我不明白为什么我需要 ()
以及 MyTypeQueOrdering
的去向。
这是 PriorityQueue
的 header ,可在 official github 上找到。 :
class PriorityQueue[A](implicit val ord: Ordering[A])
这是我的尝试(有效):
class MyType{
}
object MyTypeQueOrdering extends Ordering[MyType]{
def compare (n1:MyType, n2:MyType) = -1
}
class MyTypeQue extends PriorityQueue[MyType]()(MyTypeQueOrdering){
}
...但我不明白为什么我需要 ()
。 PriorityQueue[MyType]()
是否返回某些内容?
最佳答案
尝试将MyTypeQueOrdering
设为隐式对象
:
object Implicits {
//implicit objects can't be top-level ones
implicit object MyTypeQueOrdering extends Ordering[MyType] {
def compare(n1: MyType, n2: MyType) = -1
}
}
这样你就可以省略两个括号:
import Implicits._
class MyTypeQue extends PriorityQueue[MyType] { ... }
在示例中需要空括号的原因是 PriorityQueue[MyType](MyTypeQueOrdering)
会假设您尝试将排序作为构造函数参数传递。因此,这就是为什么您需要显式显示无参数实例化,然后传递排序
关于scala - 扩展泛型类型 - PriorityQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22665131/