scala implicit conversion and an implicit call

// implicit conversion / **

  1. Scala implicit conversion, in fact, the core is defined implicit conversion function, namely implicit conversion function. *
  2. It can not only simplify programming, it is possible to make the program very flexible definition of implicit conversion function, as long as the introduction of the programming, Scala will be automatically used. *
  3. Keywords implicit conversion is implict * implict keywords to modified function is called implicit conversion function *
  4. Implicit conversion format Terget naming style source2Terget source input format you want to get *
  5. Users do not need to manually call the hermit function, which is automatically invoked Scala.
 package scaladay02.com.aura
object ImplicitConversion {
  def main(args: Array[String]): Unit = {
//定义隐式转换将String类型的参数转为Int类型
    implicit def str2Int(str:String):Int=str.length
    val str:Int="abcdaa"
    println(str)
//将peopel进行隐式转换为int类型
    implicit def person2Int(person:People01):Int=person.getAge
   //在此处是用来隐式调用自动调用定义的隐式转换方法
    val p:Int=new People01()
println(p)
  }
}
class People01{
  //封装属性
  private var name:String=_
  private var age:Int=10
  def setName(n:String)={
    name=n
  }
  def getName()=name
  def setAge(a:Int)= {
    age = a
  }
  def getAge=age
}

scala implicit conversion custom API

package scaladay02.com.aura

import java.io.File

import scala.io.Source

/**使用隐式转换丰富现有API
  * 当现有的API无法满足我们得需求时我们可以用隐式转换定义我们需要的API
  * 也就是用隐式转换来丰富现有的API
  */

object AddAPI {
  def main(args: Array[String]): Unit = {
    class RichFile(file:File){
      //定义文件的读取
      def read={
        Source.fromFile(file).mkString
      }

    }
    //定义隐式转换
  implicit  def file2RichFile(file:File):RichFile=new RichFile(file)
  //创建对象传入文件路径
    val file=new File("data/data.log")
    //自动调用隐式转换
    println(file.read)
  }
}

Implicit conversion reference

// implicit conversion reference
/
* implicit reference conversion and general reference to the class is done by the import keyword

  • As long as the implicit conversion functions into the scope of their own access to it can automatically apply the
  • If in the present class definition can be scanned as long as the front of the implicit conversion to
    / *
package scaladay02.com.aura
import java.io.File
import scaladay02.com.aura.AddAPI._
object Quote {
  def main(args: Array[String]): Unit = {
val file=new File("data/data.log")
 println(file.read)
  }
}

Implicit parameters

*** implicit parameter

  • It refers implicit definition of a function or a modified process parameters
  • At this point scala will try to find a specific type of objects with implicit modified
  • I.e., injection and the parameter value of the implicit
  • scala will find a current scope is visible in two ranges val or var
  • Implicit values ​​within a parameter is implicit accompanying objects
package scaladay02.com.aura
object ImplicitParameters {
  def main(args: Array[String]): Unit = {
    val list = List(6, 3, 6, 2, 63, 552, 2, 2)
    list.sorted.foreach(println)
  }

  def implicitPar: Unit ={
    //定义集合
    val list = List (new People02 ("王敏", 12),
    new People02 ("王明", 10),
    new People02 ("张琴", 20),
    new People02 ("莉莉", 22) )
//定义隐式参数
    implicit val order = new Ordering[People02] () {
    override def compare (x: People02, y: People02): Int = {
  //定义隐式参数的比较条件
    y.age.compareTo (x.age)
  }

  }
    //相同类型的隐式转换参数,在可访问的作用域内不可重复,否则冲突
    /*implicit val order2 = new Ordering[Person]() {
        override def compare(x: Person, y: Person) = {
            y.name.compareTo(x.name)
        }
    }*/
list.sorted.foreach(println)
  }

}

//方法的定义
class People02{

  var name:String=_
   var age:Int=_
   def this(name:String,age:Int){
     this()
     this.age=age
     this.name=name
   }
}

Guess you like

Origin blog.csdn.net/weixin_44701192/article/details/92109007