在Java中,我可以這樣做:的構造方法參數相同的名稱,atributes
class Point{
int x, y;
public Point (int x, int y){
this.x = x;
this.y = y;
}
}
我怎樣才能做到在斯卡拉同樣的事情(在構造函數參數和類屬性使用相同的名稱):
class Point(x: Int, y: Int){
//Wrong code
def x = x;
def y = y;
}
編輯
我問這貝科使用下面的代碼不起作用
class Point(x: Int, y: Int) {
def +(that: Point): Point = new Point(this.x + that.x, this.y + that.y)
}
但接下來一個工程:
class Point(px: Int, py: Int) {
def x = px
def y = py
def +(that: Point): Point = new Point(this.x + that.x, this.y + that.y)
}
在編輯中的第一個代碼片段中,使用'val'來聲明參數,並且您的代碼有效。 – xbonez 2013-05-07 04:26:45