我可以在Scala类中定义无名方法吗?

Iva*_*van 2 methods syntax scala class

这有可能达到吗?如果是,请更正我的Foo声明语法.


class Foo (...) {
...
  def /* the nameless method name implied here */ (...) : Bar = new Bar (...)
...
}

class Bar (...) {
...
}

val foo : Foo = new Foo (...)

val fooBar : Bar = foo (...)


oll*_*erg 12

你应该使用apply方法:

class Foo (y: Int) {
  def apply(x: Int) : Int = x + y
}


val foo : Foo = new Foo (7)

val fooBar  = foo (5)

println(fooBar)
Run Code Online (Sandbox Code Playgroud)

然后运行代码:

bash$ scala foo.scala 
12
Run Code Online (Sandbox Code Playgroud)