隐式转换

10 阅读1分钟
package imp

object imp01 {
  implicit def doubleToInt(x:Double):Int = {
    println("double to int ...")
    x.toInt
  }

  def main(args: Array[String]): Unit = {
    // 1.隐式转换
    var i:Int = 1;

    var d:Double = 1.1;

    d = 1  //把int  ->  double类型

    // d = i.toDouble
    // println(s"d=${d}")

    i = d // 存在一个隐式转换    把double  ->  int  报错!!!
    i = 100.1
    i = 100.1
  }
}