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)
ten*_*shi 14
我认为在这种情况下,最好的解决方案是使用工厂方法而不是公共构造函数.
因此,您可以定义构造函数private并apply在随附对象中提供工厂方法:
class Foo private (val x:Int)
object Foo {
def apply(i: Int) = new Foo(i)
def apply(s: String) = new Foo(convertToInt(s))
def convertToInt(s: String) = s.toInt
}
println(Foo(512).x)
println(Foo("256").x)
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到有关构造函数与工厂方法的更多信息:
对于Scala来说也是如此.
作为替代解决方案的一个例子,我制作了非常通用的解 Fooclass现在可以使用任何曾经存在或可以在将来创建的类,假设这种类型可以转换(您可以定义它应该如何转换)到/从Int:
trait Convertable[From, To] {
def convert(from: From): To
}
object Convertable {
implicit val intString = new Convertable[Int, String] {
def convert(from: Int) = from toString // your logic here
}
implicit val stringInt = new Convertable[String, Int] {
def convert(from: String) = from toInt // your logic here
}
implicit def self[T] = new Convertable[T, T] {
def convert(from: T) = from
}
}
case class Foo[T](original: T)(implicit toInt: Convertable[T, Int], fromInt: Convertable[Int, T]) {
val x: Int = toInt convert original
def toOriginal = fromInt convert x
}
println(Foo(512) x)
println(Foo("256") x)
Run Code Online (Sandbox Code Playgroud)
(我可以toOriginal通过返回来定义= original,但它会太无聊:)
如您所见,此解决方案通用且更复杂.但据我所知,许多应用程序需要在不同的原始值和/或类之间进行某种转换.因此,在许多情况下,它对于许多情况而言是可靠的(并且可能被认为是非常好的事件)解决方案,并且可能对您而言也是如此.但是,对于所有可能的情况,通常无法确定什么是"最佳"解决方案.
通过使用角度关于工厂方法的报价而不是辅助构造函数:
class Foo(val s:String) {
val s = ""
def bar2:String = s+s
def bar3:List[Char] = s.toList
}
object Foo extends{
def bar1(y:List[Char]):String =y.mkString
def apply(s:String)= new Foo(s)
def apply(y:List[Char])= new Foo(bar1(y))
}
Run Code Online (Sandbox Code Playgroud)
客户端代码:
val foo1 = Foo(List('a','b'))
println(foo1.s)
println(foo1.bar2)
println(foo1.bar3)
Run Code Online (Sandbox Code Playgroud)