objectTestImplicit1{ defmain(args: Array[String]): Unit = { val stu = newStudent() implicitdeftoInt(s:String): Int ={Integer.parseInt(s)} stu.print("50") } }
功能扩展
1 2 3 4 5 6 7 8 9 10 11
package test
classTeacher{ implicitdefteach(): Unit ={ println("他在讲课!!!") } deftest(): Unit ={ println("考试!!!") } }
1 2 3 4 5 6 7 8 9 10 11 12 13
package test
objectTestImplicit1{ defmain(args: Array[String]): Unit = { val stu = newStudent() implicitdeftoTeacher(s:Student): Teacher ={ newTeacher } stu.teach() stu.test() } }