scala 隐式转换
先参考这篇文章:http://www.jianshu.com/p/a344914de895
package com.test.scalaw.test /** * scala隐式转换 */ object ImplicitConversionFun { //隐式类 implicit class StringImprovement(val s : String){ def increment = s.map(x => (x +1).toChar) } implicit def int2String(x : Int) = x.toString//int 到 String 的转换 //定义一个隐式转换变量参数,隐式变量的值可以不传 def person(implicit name : String) = {"hello"} implicit val p = "mobin" //p被称为隐式值 class PreferredDrink(name : String){} implicit val prompt = new PreferredDrink("Yes, master") def greet(name : String)(implicit prompt : PreferredDrink) = { println("yes") } //带有隐式参数的函数 def maxList[T](elements : List[T])(implicit orderer:T => Ordered[T]):T= elements match{ case List()=> throw new IllegalArgumentException case List(x)=>x case x:: rest=> val maxRest=maxList(rest) if(x>maxRest) x else maxRest } def main(args: Array[String]): Unit = { println(person("hahaha")) person person(p) greet(p) } }