2016-08-12 8 views
0

私はスカラを初めて知り、スカラを学ぼうとしています。 2つのクラスの下に書きますが、以下のエラーが表示されます。 はあなたが二回topleftbottomrightを定義している私scala errorオーバーロードされた定義へのあいまいな参照

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

TOPLEFTとbottomrightせずに、以下のようRectangleクラスを定義してください: -

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

を助けてください。単にエラーを修正するには、以下の2行を削除します。

def topleft: Point 
def bottomright: Point 
+0

をmarstranおかげで、わずか1つの詳細を知りたいクラスに私がval topleftを使用しました:Point、val bottomright:valをポイント、def topleftを指定します。Point | def bottomright:メソッドとしてのポイント、スカラーでvalとdefに同じ名前を使うことはできません。メソッドは – subho

+0

いいえ、できません。あなたがそれを参照するときにどのようなものが分かっていますか? 'def'はあなたがそれを使うたびに評価される式です。 'val'は一度だけ評価される式で、すぐに起こります。 – marstran

+0

okありがとうMarstran – subho