为什么这个参考模糊不清?

Lui*_*hys 8 scala scala-swing

import swing._

object PeerTest extends SimpleSwingApplication {
  def top = new MainFrame {
    val p = peer.getMousePosition 
  }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

但添加类型

val p: Point = peer.getMousePosition 
Run Code Online (Sandbox Code Playgroud)

没事.为什么?

编辑:导致问题:

class A {
  def value() = 123
}

class B extends A {
  def value(b: Boolean) = 42  
}

object Main extends App {
  println ((new B).value) 
}
Run Code Online (Sandbox Code Playgroud)

不会引起问题:

class A {
  def value() = 123
  def value(b: Boolean) = 42  
}

class B extends A {}

object Main extends App {
  println ((new B).value) 
}
Run Code Online (Sandbox Code Playgroud)

所以我认为答案必须解释为什么只有当方法在不同的类中时才会发生.

Jen*_*der 10

有两种方法,getMousePosition一种没有,一种有布尔参数.

如果没有类型注释,Scala不知道您是否希望在一个参数(Function1对象)中引用该方法,或者您想要调用不带参数的方法(导致a Point).

指定预期类型可明确您的意图.

使用也getMousePosition()应该工作.