scala错误模糊引用重载定义

时间:2016-08-12 11:16:31

标签: scala

我是scala的新手并且正在尝试学习scala。 我试着写下2个类,但得到以下错误。 请帮帮我

scala> class Point(val x: Int,val y: Int)
defined class Point

scala> class Rectangle(val topleft: Point,val bottomright: Point){
     | def topleft: Point
     | def bottomright: Point
     | def left=topleft.x
     | def right=bottomright.x
     | def width=right-left
     | }


<console>:14: error: ambiguous reference to overloaded definition,
both value topleft in class Rectangle of type => Point
and  method topleft in class Rectangle of type => Point
match expected type ?
       def left=topleft.x
                ^


<console>:15: error: ambiguous reference to overloaded definition,
both value bottomright in class Rectangle of type => Point
and  method bottomright in class Rectangle of type => Point
match expected type ?
       def right=bottomright.x
                 ^
<console>:13: error: value bottomright is defined twice
  conflicting symbols both originated in file '<console>'
       def bottomright: Point
           ^

<console>:12: error: value topleft is defined twice
  conflicting symbols both originated in file '<console>'
       def topleft: Point

谢谢和问候,

2 个答案:

答案 0 :(得分:3)

您已定义topleftbottomright两次。只需删除以下两行即可修复错误:

def topleft: Point
def bottomright: Point

答案 1 :(得分:2)

请在下面定义Rectangle类,而不要使用topleft和bottomright: -

scala> class Rectangle(val topleft: Point,val bottomright: Point){
     | def left=topleft.x
     | def right=bottomright.x
     | def width=right-left
     | }
defined class Rectangle