implicit class StrongString(str: String) {def isPhone: Boolean = {val reg = "1[3-9]\\d{9}".rreg.matches(str)}}def main(args: Array[String]): Unit = {val str: String = "18888488488"// 需求:给字符串补充一个功能isPhone,判断它是不是一个手机号val rs = str.isPhone//val rs = (new StrongString(str)).isPhoneprintln(s"$str 的检查结果是:$rs")}/* object StringExtension {// 私有类,外部无法直接访问其构造函数等细节private class StrongString(private val str: String) {def isPhone: Boolean = {val reg = "1[3-9]\\d{9}".rreg.matches(str)}}// 私有作用域内的隐式转换,限制其使用范围,使其更符合局部使用场景private[StringExtension] implicit def stringToStringString(str: String): StrongString = {new StrongString(str)}def checkPhone(str: String): Boolean = {// 通过隐式转换调用isPhone方法进行判断str.isPhone}def main(args: Array[String]): Unit = {val str: String = "18888488488"val rs = checkPhone(str)println(s"$str 的检查结果是:$rs")}}*/