当前位置: 首页 > news >正文

网站开发大概价格建设电子商务网站流程

网站开发大概价格,建设电子商务网站流程,WordPress实验室,论坛建站教程模式匹配是检查某个值(value)是否匹配某一个模式的机制,一个成功的匹配同时会将匹配值解构为其组成部分。它是Java中的switch语句的升级版,同样可以用于替代一系列的 if/else 语句。 语法 一个模式匹配语句包括一个待匹配的值&a…

模式匹配是检查某个值(value)是否匹配某一个模式的机制,一个成功的匹配同时会将匹配值解构为其组成部分。它是Java中的switch语句的升级版,同样可以用于替代一系列的 if/else 语句。

语法

一个模式匹配语句包括一个待匹配的值,match关键字,以及至少一个case语句。

// 导入随机数生成器
import scala.util.Random// 定义主对象
object Main {// 主方法,程序入口def main(args: Array[String]): Unit = {// 生成一个0到9之间的随机整数val x:Int = Random.nextInt(10)// 使用模式匹配根据随机数的值执行不同的操作x match {// 当x为0时输出"zero"case 0 => println("zero")// 当x为1时输出"one"case 1 => println("one")// 当x为2时输出"two"case _ => println("other") // 其他情况输出"other"}}
}

上述代码中的val x是一个0到10之间的随机整数,将它放在match运算符的左侧对其进行模式匹配,match的右侧是包含4条case的表达式,其中最后一个case _表示匹配其余所有情况,在这里就是其他可能的整型值。

match表达式具有一个结果值

  def matchTest(x: Int): String = x match {case 1 => "one"case 2 => "two"case _ => "other"}println(matchTest(3)) // otherprintln( matchTest(1)) // one

这个match表达式是String类型的,因为所有的情况(case)均返回String,所以matchTest函数的返回值是String类型。

样例类(case classes)的匹配

样例类非常适合用于模式匹配。

abstract class Notificationcase class Email(sender: String, title: String, body: String) extends Notificationcase class SMS(caller: String, message: String) extends Notificationcase class VoiceRecording(contactName: String, link: String) extends Notification

Notification 是一个虚基类,它有三个具体的子类Email, SMSVoiceRecording,我们可以在这些样例类(Case Class)上像这样使用模式匹配:

def showNotification(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"}
}
val someSms = SMS("12345", "Are you there?")
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")println(showNotification(someSms))  // prints You got an SMS from 12345! Message: Are you there?println(showNotification(someVoiceRecording))  // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123

showNotification函数接受一个抽象类Notification对象作为输入参数,然后匹配其具体类型。(也就是判断它是一个EmailSMS,还是VoiceRecording)。在case Email(sender, title, _)中,对象的sendertitle属性在返回值中被使用,而body属性则被忽略,故使用_代替。

模式守卫(Pattern guards)

为了让匹配更加具体,可以使用模式守卫,也就是在模式后面加上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))

case Email(sender, _, _) if importantPeopleInfo.contains(sender)中,除了要求notificationEmail类型外,还需要sender在重要人物列表importantPeopleInfo中,才会匹配到该模式。

仅匹配类型

也可以仅匹配类型,如下所示:

abstract class Device
case class Phone(model: String) extends Device {def screenOff = "Turning screen off"
}
case class Computer(model: String) extends Device {def screenSaverOn = "Turning screen saver on..."
}def goIdle(device: Device) = device match {case p: Phone => p.screenOffcase c: Computer => c.screenSaverOn
}

当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用,如上代码中goIdle函数对不同类型的Device有着不同的表现。一般使用类型的首字母作为case的标识符,例如上述代码中的pc,这是一种惯例。

密封类

特质(trait)和类(class)可以用sealed标记为密封的,这意味着其所有子类都必须与之定义在相同文件中,从而保证所有子类型都是已知的。

sealed abstract class Furniture
case class Couch() extends Furniture
case class Chair() extends Furnituredef findPlaceToSit(piece: Furniture): String = piece match {case a: Couch => "Lie on the couch"case b: Chair => "Sit on the chair"
}

这对于模式匹配很有用,因为我们不再需要一个匹配其他任意情况的case

http://www.dtcms.com/a/495213.html

相关文章:

  • Python 练习脚本(从基础到高级150个练习)
  • GDDR6总结(1)-背景及优劣
  • Redis 中文学习手册
  • 网站改版 程序变了 原来的文章内容链接地址 打不开怎么办以百度云做网站空间
  • iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
  • 莞城做网站wordpress 插件数据
  • YouTube 视频评论,并插入 MySQL
  • mac idea 点击打开项目卡死
  • 网站建设座谈会上的发言wordpress显示文章点击量
  • 室内设计效果图网站推荐在线玩网页游戏h5网站大全
  • C# 仿QQ聊天功能实现 (SQL Server数据库)
  • TensorFlow深度学习实战——节点分类
  • scipy的统计学库(4):用rv_histogram类实现随机抽样
  • Element Plus el-table 默认勾选行的方法
  • Linux系统函数opendir、closedir、readdir详解及案例(自定义ls工具)
  • 便捷网站建设哪家便宜网站建没有前景
  • 接口测试 | Postman的高级用法的测试使用
  • TR3--Transformer之pytorch复现
  • Traccar本地文件包含漏洞(CVE-2025-61666)
  • 建站网站推荐icp域名备案查询系统
  • 智能美颜引擎:美颜SDK如何实现自适应芯片性能优化
  • Java中的boolean与Boolean
  • Flutter高级进阶教程(视频教程)
  • Rocketmq 分布式事务 两阶段提交
  • 骑行,团骑和独骑冲突吗?
  • 对网站和网页的认识鞍山信息网便民信息
  • 《算法通关指南---C++编程篇(2)》
  • 【论文速递】2025年第29周(Jul-13-19)(Robotics/Embodied AI/LLM)
  • 网站 模板更改网站备案
  • VR反诈一体机-VR预防诈骗模拟系统-VR防诈骗体验馆方案