Scala类访问public var

时间:2015-06-06 20:04:17

标签: scala

class Cliente() {
  var Prato: Prato
  var Bebidas: Set[Bebida]
  var Sobremesas: Set[Sobremesa]
}

如果我打电话

cliente = new Cliente()
cliente.Prato = Pratos(opt)

我得到了

error: class Cliente needs to be abstract, since:
it has 3 unimplemented members.
/** As seen from class Cliente, the missing signatures are as follows.
 *  For convenience, these are usable as stub implementations.
 */
  def Bebidas_=(x$1: Set[this.Bebida]): Unit = ???
  def Prato_=(x$1: this.Prato): Unit = ???
  def Sobremesas_=(x$1: Set[this.Sobremesa]): Unit = ???

我在scala中没有公共属性?

1 个答案:

答案 0 :(得分:1)

在Java领域,我们习惯于为未初始化的字段设置默认值。然而,在Scala土地上,我们无法做到。

我们被迫分配值 - 即使它们是null_ - 而不是像Java土地那样允许它们默认为null。这在某种程度上可以避免我们在Java领域遇到的NullPointerException问题。