Scala——模式匹配;Option类(可选类)的使用;样例类的定义与使用;隐式函数实现对象转换

模式匹配

模式匹配的功能非常强大,使用恰当的话可以省略很多不必要的操作

/**
  * 模式匹配使用
  */
object PatternDemo {
  def main(args: Array[String]): Unit = {
    judgeGrade("F","zs")

   val list: List[(String, (String, Int))] =  List(("A",("a",1)),("B",("B",2)),("C",("c",3)))

    //获取List中int类型数据
    list.map(tuple => tuple._2._2)

    //使用模式匹配
    val xx: List[Int] = list.map{
      case (ip,(name,age)) =>age
    }

    val list2: List[Array[Any]] = List(Array("sz", 24, 34.98), Array("ls", 23, 90.09), Array("ww", 34, 12.89))

    list2.map{
      case Array(name,orderId,money) => money
    }

    val logInfo  = "zhangsan,34,male,177777"
    val Array(name,age,sex,telphone)  = logInfo.split(",")
    println(s"$name")

  }

  /**
    * 根据 学生的成绩(类别A,B,C,D)等级,给出不同的评语
    */
  def judgeGrade(grade:String):Unit ={
    grade match {
      case "A" =>println("excellent.......")
      case "B" =>println("good......")
      case "C" =>println("just so so ......")
      case _ =>println("you need to work hader")
    }
  }
  def judgeGrade(grade:String,name:String):Unit ={
    grade match {
      case "A" =>println("excellent.......")
      case "B" =>println("good......")
      case "C" =>println("just so so ......")
      case _grade if "zs".equals(name) =>println(s"just so so ${_grade}")
      case _ =>println("you need to work hader")
    }
  }
}

Option类(可选类)的使用

/**
  * Option有2个子类
  *  -some
  *   表示有值
  *  -none
  *  表示无值
  */
object OptionDemo {
  def main(args: Array[String]): Unit = {
    val map = Map("A"->1,"B"->2)

    val opt: Option[Int] = map.get("A")
    //val optValue = if(opt.isDefined){ opt.get}
    val optValue = if(opt.isDefined) opt.get

    val x: Int = map.get("A") match {
      case Some(value) =>value
      case None => 0
    }

    /**
      * def getOrElse[B1 >: B](key: A, default: => B1): B1 = get(key) match {
      * case Some(v) => v
      * case None => default
      * }
      */
    val xx = map.getOrElse("A",0)
  }
}

样例类的定义与使用

/**
  * 定义 样例类
  *
  */
case class AAA(name:String,age:Int)

//相当于下面这段代码
class AA(name:String,age:Int)
object AA{
  def apply(name: String, age: Int): AA = new AA(name, age)
}

class People
case class Student(name:String,classRoom:String) extends People
case class Teacher(name:String,subject:String) extends People

object caseClassDemo {

  def main(args: Array[String]): Unit = {
    val  aaa = AAA("zs",18)

    check(Teacher("xx","java"))
  }

  def check(people: People): Unit ={
    people match {
      case Teacher(name,subject)=>println("teacher")
      case Student(name,classRoom)=>println("Student")
      case _ =>println("==================")
    }
  }
}

隐式函数实现对象转换

/**
  * 隐式函数
  * 可以让一个对象偷偷的变身
  * 变身函数:
  *     (0)变身函数被implicit修饰,函数名任意
  *     (1)变身函数一般在:伴生对象中
  */


//普通人
class Man(val name: String)
//object Man{
//  implicit def man2SuperMan(man:Man):SuperMan={
//    new SuperMan(man.name)
//  }
//}

object AAA{
  implicit def man2SuperMan(man:Man):SuperMan={
    new SuperMan(man.name)
  }
}

//超人:奥特曼
class SuperMan(val name: String){
  //发射激光
  def emitLaser():Unit= println("emit a laser.........")
}

object ImplicitDemo {
  def main(args: Array[String]): Unit = {
    val man = new Man("super")

    /**
      * 默认情况下:
      *   找变身函数 当前可见的作用域里面找,
      *         原类型伴生对象中找 (推荐用法)
      *         当前类下面找
      *         手动导入
      */
    import com.huadian.bigdata.oop.demo08.AAA._
    man.emitLaser()
  }
//  implicit def man2SuperMan(man:Man):SuperMan={
//    new SuperMan(man.name)
//  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名一小卒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值