class foo(val x:Int){
def convertToInt(z:string) = {do somthing to convert a string to an integer}
def this(y:string) = this(convertToInt(y))
}
Run Code Online (Sandbox Code Playgroud)
在辅助构造函数中调用convertToInt(this(y:string))会导致此错误:
error: not found: value convertToInt
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用单例对象并将所有静态函数(如convertToInt)打包到其中,但这是一个很好的解决方案吗?
object foo{
def convertToInt(z:string) = {do somthing to convert a string to an integer}
}
class foo(val x:Int){
def this(y:string) = this(foo.convertToInt(y))
}
Run Code Online (Sandbox Code Playgroud) scala ×1