code

package com.aura.scala.day01

object patternMatching02 {

  def main(args: Array[String]): Unit = {
def showNotifacation(notification: Notification):String = {
notification match {
case Email(sender, title, _) => s"You got an email from $sender with title : $title"
case SMS(number, message) => s"You got an SMS from $number! Message:$message"
case VoiceRecording(name, link) => s"you received a Voice Recording from $name! Click the link to hear it: $link"
case _ => "other"
}
}
val someSms = SMS("", "Are you there?")
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
println(showNotifacation(someSms))
println(showNotifacation(someVoiceRecording))
}
} abstract class Notification
case class Email(sender: String,title: String, body: String) extends Notification
case class SMS(caller: String, message: String) extends Notification
case class VoiceRecording(contactName: String, link: String) extends Notification

resule :

"D:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=5964:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF- -classpath "D:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;C:\Users\admin\IdeaProjects\ScalaDay\out\production\ScalaDay;C:\Program Files (x86)\scala\lib\scala-actors-2.11.0.jar;C:\Program Files (x86)\scala\lib\scala-actors-migration_2.11-1.1.0.jar;C:\Program Files (x86)\scala\lib\scala-library.jar;C:\Program Files (x86)\scala\lib\scala-parser-combinators_2.11-1.0.4.jar;C:\Program Files (x86)\scala\lib\scala-reflect.jar;C:\Program Files (x86)\scala\lib\scala-swing_2.11-1.0.2.jar;C:\Program Files (x86)\scala\lib\scala-xml_2.11-1.0.4.jar" com.aura.scala.day01.patternMatching02
You got an SMS from ! Message:Are you there?
you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/ Process finished with exit code

scala 模式匹配也可以在模式后面加上if <boolean expression> 实现模式守卫。

learning scala pattern matching 02的更多相关文章

  1. learning scala pattern matching 03

    code: package com.aura.scala.day01 object patternMatching03 { //当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用. def g ...

  2. learning scala pattern matching

    code: package com.aura.scala.day01 import scala.util.Random object patternMatching01 { def main(args ...

  3. scala pattern matching

    scala语言的一大重要特性之一就是模式匹配.在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中, ...

  4. [Scala] Pattern Matching(模式匹配)

    Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...

  5. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

  6. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  7. 论文笔记之:MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching

    MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching CVPR  2015 本来都写到一半了,突然笔记本死机了 ...

  8. Zhu-Takaoka Two-dimensional Pattern Matching

    Two dimensional pattern matching. Details may be added later.... Corresponding more work can be foun ...

  9. 配置和运行 MatchNet CVPR 2015 MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching

    配置和运行 MatchNet CVPR 2015 GitHub: https://github.com/hanxf/matchnet 最近一个同学在配置,测试这个网络,但是总是遇到各种问题. 我也尝试 ...

随机推荐

  1. 【C#】课堂知识点#3

    1.讲解了实验1中,利用Char.is***来进行判断字符类型. using System; using System.Collections.Generic; using System.Linq; ...

  2. vscode之万里挑一

    前置 我们经常在工作区中打开很多项目文件夹,找起来比较麻烦. 步骤

  3. VS.NET(C#)--1.3_VS2005开始

    VS2005开始 开始页 1.文件系统:这是默认,把网站创建到当前物理文件系统上(可以本地或网络).此时VS2005将使用内置的Web服务器,不使用IIS运行Web应用程序.2.HTTP使用IIS处理 ...

  4. 一行css代码搞定响应式布局

    在这篇文章中,我将教你如何使用 CSS Grid 来创建一个超酷的图像网格图,它将根据屏幕的宽度来改变列的数量.最精彩的地方在于:所有的响应特性被添加到了一行 css 代码中.这意味着我们不必将 HT ...

  5. python day3 int,str,list类型补充

    目录 python day 3 1. int类小知识点 2. str类小知识点 3. list类小知识点 python day 3 (学习资料来自老男孩教育) 2019/10/06 1. int类小知 ...

  6. nginx的access_log与error_log

     参考文章:https://juejin.im/post/5aa09bb3f265da238f121b6c 本篇文章主要介绍一下 nginx 服务器两种日志查看:access_log.error_lo ...

  7. dvaJS Model之间的调用

    const Model: ModelType = { namespace: 'namesps', state: { data: {} }, effects: { *fetch({ payload, c ...

  8. 安卓开发之生成cache目录和files目录

    package com.lidaochen.test; import android.os.Bundle; import android.support.v7.app.AppCompatActivit ...

  9. python OpenCV使用

    关于OpenCV简介  OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效——由一系列 C ...

  10. PX4/Pixhawk uORB

    PX4/Pixhawk的软件体系结构主要被分为四个层次 应用程序的API:这个接口提供给应用程序开发人员,此API旨在尽可能的精简.扁平及隐藏其复杂性 应用程序框架:这是为操作基础飞行控制的默认程序集 ...