在 Scala 中,我可以创建一个采用多个参数列表的方法:
def myMethod(value: Int)(fn: (Int) => Unit) {
fn(value)
}
然后这样调用它:
myMethod(10) { value => println(value) }
如何使用类构造函数做同样的事情?我怎么调用它?
最佳答案
这行得通。
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class MyClass(val value: Int)(val fn: Int => Unit)
defined class MyClass
scala> new MyClass(10)({value => println(value)})
res0: MyClass = MyClass@17577f9
关于scala - 使用多个参数列表调用 Scala 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778953/