一、编写隐式转换类

/**
  * Author Mr. Guo
  * Create 2019/4/20 - 17:40
  */
object StringImprovments {

  implicit class StringImprove(s: String) {
    def increment = s.toString.map(c => (c + 1).toChar)
  }

  implicit class Intc(i: Int) {
    def xx = {
      Integer.parseInt(i.toString) + 4
    }
  }

  implicit class arrStrToArrInt(arr: Array[String]) {
    def toArrInt = {
      arr.map(arr => arr.toInt)
    }
  }

  implicit class arrStrToArrDouble(arr: Array[String]) {
    def toArrDouble: Array[Double] = {
      arr.map(ar => {
        try {
          ar.toDouble
        } catch {
          case x: Exception => 0.0
        }
      })
    }
  }
}

二、隐式函数的调用

/**
  * Author Mr. Guo
  * Create 2019/4/20 - 16:44
  */
object OperatorStr {

  def operatorStr() = {
    import unitlOne.StringImprovments._
    val str2 = "HCL"
    val int1 = 3
    val arrs = Array[String]("")
    println(str2.increment)
    println(int1.xx)
  }
}

  

相关文章:

  • 2021-10-19
  • 2022-12-23
  • 2021-09-14
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-12
  • 2021-08-20
  • 2021-07-19
  • 2021-09-27
  • 2022-02-01
相关资源
相似解决方案