想不通有什么错此代码:scala 2.10:为什么会出现类型不匹配?
trait NumberLike
{
def plus[T](a: T, b: T): T
}
class IntegerNumberLike extends NumberLike
{
def plus[Int](a: Int, b: Int): Int = 2 // type mismatch; found : scala.Int(2) required: Int
}
但如果我这样做,这样一来,它的工作原理:
trait NumberLike[T]
{
def plus(a: T, b: T): T
}
class IntegerNumberLike extends NumberLike[Int]
{
def plus(a: Int, b: Int): Int = 2
}
所以,我有两个问题:
- 为什么第一个代码示例不起作用?
- 一般来说,何时应该使用类类型参数,何时应该使用方法类型参数?