gpt4 book ai didi

scala - 使用多个参数列表调用 Scala 构造函数

转载 作者:行者123 更新时间:2023-12-02 06:42:14 27 4
gpt4 key购买 nike

在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com