import swing._
object PeerTest extends SimpleSwingApplication {
def top = new MainFrame {
val p = peer.getMousePosition
}
}
error: ambiguous reference to overloaded definition,
both method getMousePosition in class Container of type (x$1: Boolean)java.awt.Point
and method getMousePosition in class Component of type()java.awt.Point
match expected type ?
val p = peer.getMousePosition
,但增加的类型
val p: Point = peer.getMousePosition
使得确定。为什么?
编辑:导致问题:
class A {
def value() = 123
}
class B extends A {
def value(b: Boolean) = 42
}
object Main extends App {
println ((new B).value)
}
不会造成问题:
class A {
def value() = 123
def value(b: Boolean) = 42
}
class B extends A {}
object Main extends App {
println ((new B).value)
}
所以我想答案必须解释为什么只有当这些方法在不同的班级出现。
因此,如果我们决定向类中添加重载方法,我们可以打破人的代码......这看起来不太好。我认为在方法之后加上'_'是使它成为部分应用函数的方法。 –
@Luigi有很多方法通过添加重载来打破人们的代码,这只是一种方法。 –
http://stackoverflow.com/questions/2510108/why-avoid-method-overloading – retronym