zoukankan      html  css  js  c++  java
  • Scala 入门笔记

        name match {
          case "aaa" => println("this is aaa")
          case "bbb" => println("this is bbb")
          case _ => println("lala")
    
        }
    
    
    // 匹配类型
    case str: String => ....
    case int: Int => ....
    case matchType: MatchType => .... // 匹配自定义类型
    
      // 自定义类型
      class MatchType {
      }

    匹配数组,元组,集合

    val arr = Array(1, 2, 3, 6)
    
    arr match {
      case Array(3, a, b, c) => println(s"case: $a, $b, $c")
      case Array(_, x, y, z) => println(s"case: $x, $y, $z")
      case _ => println("no matched")
    }
    
    val tup = ("a", 1, 4)
    
    tup match {
      case ("c", b, 4) => println(s"case:  $b")
      case ("a", x, y) => println(s"case: $x, $y")
      case _ => println("no matched")
    }
    
    val list1 = List(0, 1, 2, 3)
    
    list1 match {
        // Nil 是个空List, :: 从右向左运算 (0 :: Nil 生成一个List(0))
      case 0 :: Nil => println("case1: 0")
      case 2 :: a :: b :: c :: d :: Nil => println(s"case2: $a, $b, $c, $d")
      case 0 :: a => println(s"case3: $a")
      case List(0, a, b, c) => println(s"case4: $a, $b, $c")
      case _ => println("hahaha")
    }
    
    // 匹配样例类
    object CaseClassDemo {
      def main(args: Array[String]): Unit = {
        val arr = Array(CheckTimeOutTask, SubmitTask("1000", "task-0001"), HeartBeat(3000))
    
        arr(Random.nextInt(arr.length)) match {
          case CheckTimeOutTask => println("CheckTimeOutTime")
          case SubmitTask(port, name) => println("SubmitTask")
          case HeartBeat(time) => println("HeartBeet")
        }
      }
    }
    
    case class HeartBeat(time: Long)
    case class SubmitTask(id: String, taskName: String)
    case object CheckTimeOutTask
    

      

    模式守卫

    为了让匹配更加具体,可以使用模式守卫,也就是在模式后面加上if <boolean expression>

    def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = {
      notification match {
        case Email(sender, _, _) if importantPeopleInfo.contains(sender) =>
          "You got an email from special someone!"
        case SMS(number, _) if importantPeopleInfo.contains(number) =>
          "You got an SMS from special someone!"
        case other =>
          showNotification(other) // nothing special, delegate to our original showNotification function
      }
    }
    
    val importantPeopleInfo = Seq("867-5309", "jenny@gmail.com")
    
    val someSms = SMS("867-5309", "Are you there?")
    val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
    val importantEmail = Email("jenny@gmail.com", "Drinks tonight?", "I'm free after 5!")
    val importantSms = SMS("867-5309", "I'm here! Where are you?")
    
    println(showImportantNotification(someSms, importantPeopleInfo))
    println(showImportantNotification(someVoiceRecording, importantPeopleInfo))
    println(showImportantNotification(importantEmail, importantPeopleInfo))
    println(showImportantNotification(importantSms, importantPeopleInfo))
    

      

    def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = { notification match { caseEmail(sender, _, _) if importantPeopleInfo.contains(sender) => "You got an email from special someone!"caseSMS(number, _) if importantPeopleInfo.contains(number) => "You got an SMS from special someone!"case other => showNotification(other) // nothing special, delegate to our original showNotification function } } val importantPeopleInfo = Seq("867-5309", "jenny@gmail.com") val someSms = SMS("867-5309", "Are you there?") val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123") val importantEmail = Email("jenny@gmail.com", "Drinks tonight?", "I'm free after 5!") val importantSms = SMS("867-5309", "I'm here! Where are you?") println(showImportantNotification(someSms, importantPeopleInfo)) println(showImportantNotification(someVoiceRecording, importantPeopleInfo)) println(showImportantNotification(importantEmail, importantPeopleInfo)) println(showImportantNotification(importantSms, importantPeopleInfo))

  • 相关阅读:
    Hibernate 实体关联关系映射----总结
    原型模式(Prototype)解析例子
    Java 泛型的理解与等价实现
    Java:对象创建和初始化过程
    iReport的简单配置
    Python语言程序设计基础(第2版)课后习题答案 嵩天、礼欣、黄天羽版 高等教育出版社 试题和答案和解析
    编程精品教材:MATLAB程序设计与应用(第3版) 课后答案 刘卫国版 课后习题答案解析
    SkyWalking 快速接入实践
    Java基础 day01【前言、入门程序、常量、变量】
    python最新笔试题
  • 原文地址:https://www.cnblogs.com/sunnystone85/p/11363659.html
Copyright © 2011-2022 走看看