Wyobraźmy sobie ten prosty kawałek kodu:Scala problemem opcjonalnie konstruktor
class Constructor() {
var string: String = _
def this(s: String) = {this() ; string = s;}
def testMethod() {
println(string)
}
testMethod
}
object Appl {
def main(args: Array[String]): Unit = {
var constructor = new Constructor("calling elvis")
constructor = new Constructor()
}
}
wynik jest
null
null
Chciałbym być
calling elvis
null
Jak to osiągnąć? Nie mogę wywołać metody testMethod po utworzeniu obiektu.
Mazi