scala Scala编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9471927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala compilation error
提问by Vadim Samokhin
Can't figure out what's wrong with StrangeIntQueue extending Queue, why there is an error "Not enough arguments for constructor Queue: (leading: Int)list.Lister.Queue[Int]. Unspecified value parameter leading". How can I specify it?
无法弄清楚 StrangeIntQueue 扩展队列有什么问题,为什么会出现错误“构造函数队列的参数不足:(前导:Int)list.Lister.Queue[Int]。未指定值参数前导”。我该如何指定?
class Queue[+T](
private val leading: T
) {
def enqueue[U >: T](x: U) =
new Queue[U](leading: U) // ...
}
class StrangeIntQueue(private val leading: Int) extends Queue[Int] {
override def enqueue(x: Int) = {
println(math.sqrt(x))
super.enqueue(x)
}
}
回答by Rex Kerr
extends Queue[Int](leading)
You have to pass on the arguments even if it seems "obvious" what to do.
即使看起来“显而易见”要做什么,您也必须传递参数。
Note also that since you have declared leadingprivate, you'll actually get two copies: one for StrangeIntQueueand one for Queue. (Otherwise you could have just StrangeIntQueue(leading0: Int) extends Queue[Int](leading0)and use the inherited copy of leadinginside.)
另请注意,由于您已声明为leadingprivate,您实际上会得到两份副本:一份 forStrangeIntQueue和一份 for Queue。(否则,您可以只StrangeIntQueue(leading0: Int) extends Queue[Int](leading0)使用继承的leadinginside副本。)
回答by Jesper
The primary constructor of class Queue, which StrangeIntQueueextends, takes a parameter, but you're not passing it anything for the parameter. Try this:
class 的主要构造函数Queue,它StrangeIntQueue扩展,接受一个参数,但你没有为该参数传递任何东西。试试这个:
class StrangeIntQueue(leading: Int) extends Queue[Int](leading) {
// ...
}

