//转自: https://www.youtube.com/watch?v=HhqNJuDApEo
//Define Functions in Scala using Anonymous Inner Class
//When calling a function we indirectly invoke the apply method on the object that represents the function.
//We can use the anonymous class syntax for defining a new anonymous inner class that extends the Function */
object scalatest{
def main(args: Array[String]) : Unit = {
val func = new Function1[Int , Int]
{
val a = "hello"
def apply(num:Int) = 2 * num
}
//or
/*
val func = new {
val a = "hello"
def apply(num:Int) = 2 * num
}
*/
println(func(4))
println(func.a)
}
}
Define Functions in Scala using Anonymous Inner Class
最新推荐文章于 2022-05-02 15:23:37 发布