7

For example, in Ruby I can write

def initialize(price)
    @Current_price = price
end
4

1 回答 1

15

在 scala 中,您将所有初始化都写在类/特征/对象主体中:

class Foo(price: Int) {
  val currentPrice = price
}

或者干脆

class Foo(val currentPrice: Int) {

}

正如 DNA 所说,您可以将类体视为主要的构造方法。

于 2012-07-24T22:57:44.267 回答