class Student { val mySayHello1: String => Unit = sayHello
val mySayHello2: (String, Int) => Unit = sayHello
val myTalkTo = talkTo _
val myJumpStart: () => String = jumpStart def sayHello(name: String) = {
println("Hello 1, " + name)
} def sayHello(name: String, age: Int) = {
println("Hello 2, " + name + "," + age)
} def talkTo(name: String, age: Int): Unit = {
println("Talk to , " + name + "," + age)
} def jumpStart(): String = {
"Start to Jump"
} } object FunctionDemo { def main(args: Array[String]): Unit = {
val s = new Student()
//操作标记法调用方法
s sayHello "bill"
//操作标记法调用方法
s sayHello ("bill", 20)
//函数类型赋值
s.sayHello("bill")
s.sayHello("bill", 20)
//用通配符为函数赋值
s.myTalkTo("bill", 10)
//无输入参数函数赋值
println(s.myJumpStart()) }
}
Hello 1, bill
Hello 2, bill,20
Hello 1, bill
Hello 2, bill,20
Talk to , bill,10
Start to Jump