在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)
感谢帮助.