小编Joz*_*cik的帖子

如何在Scala中的构造函数中设置实例成员?

在Scala中,我需要像Java中的代码一样:

public class A {
  private String text;  
  public A(String text) {
    this.text = text;
  }
}
Run Code Online (Sandbox Code Playgroud)

如何在Scala中实现这一目标?

我知道我可以使用class A(text: String) { ... },但这只是一个非常简单的例子而不是真实案例.

我尝试了以下内容,它始终打印为null:

class A {
  var text: String = null
  def this(text: String) = {
    this()
    this.text = text
  }
  println(text)
}
Run Code Online (Sandbox Code Playgroud)

感谢帮助.

constructor scala member instance

1
推荐指数
1
解决办法
4062
查看次数

标签 统计

constructor ×1

instance ×1

member ×1

scala ×1