Scala的隐式转换(1)

复制代码
package hfd
//需求:
//完成一个功能,让所有的字符串都能调用isPhone方法,来校验自己是不是一个手机号
object Test37_1 {
  class StrongString(val str: String) {

    //开始你的代码
    def isPhone(): Boolean = {
      val reg = "1[3-9]\\d{9}".r
      reg.matches(str)
    }
  }
  //定义一个特殊的转换函数,把str===>StrongString的对象
  def tranform(str: String): StrongString = {
    new StrongString(str)

  }

  def main(args: Array[String]): Unit = {
    val str = "12345678"
    //  println(isPhone(str))
    // println(isPhone("abc"))
    println(tranform("abc").isPhone())
    // println((new StrongString(str)).isPhone())
    //    str.isPhone()//==>true
    //    "136a".isPhone()//==>false
  }
}
相关推荐
C嘎嘎嵌入式开发3 小时前
(2)100天python从入门到拿捏
开发语言·python
Stanford_11063 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
Vallelonga4 小时前
Rust 中的数组和数组切片引用
开发语言·rust
Kiri霧4 小时前
Rust模式匹配详解
开发语言·windows·rust
white-persist5 小时前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse5 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Lucis__6 小时前
再探类&对象——C++入门进阶
开发语言·c++
007php0076 小时前
某大厂跳动面试:计算机网络相关问题解析与总结
java·开发语言·学习·计算机网络·mysql·面试·职场和发展
lsx2024066 小时前
HTML 字符集
开发语言
很㗊6 小时前
C与C++---类型转换
c语言·开发语言