有没有办法重载一个构造函数而不仅仅是一个单行构造函数?似乎在重载的构造函数中放置一个以上的语句会产生错误Application does not take parameters。例如,如果主构造函数采用 a String,则以下将起作用:
def this(num: Int) = {
this(num.toString())
}
Run Code Online (Sandbox Code Playgroud)
但是,以下情况不会:
def this(num: Int) = {
val numAsString = num.toString()
this(numAsString)
}
Run Code Online (Sandbox Code Playgroud)