代码
import scala.util.Random//做一个信息的甄别
object Patter2 {def main(args: Array[String]): Unit = {abstract class NotFaction//定义不同信息的样例类case class Email(send:String,title:String,body:String) extends NotFactioncase class SMS(caller:String,message:String) extends NotFactioncase class VoiceRecording(contactName:String,link:String) extends NotFaction//信息识别def showNotFaction(notifiction:NotFaction):String={notifiction match {case Email(send, title,_)if(send=="张三")=>"you get a Email message from"+sendcase SMS(caller, message)=>"you get a SMS message from"+callercase VoiceRecording(contactName, link)=>"you get a VoiceRecording message from"+contactNamecase _=>"you get a message not important"}}//创建一条信息val email1=Email("张三","important","some xxixxxx")val email2=Email("李四","important","some xxixxxx")println(showNotFaction(email1))println(showNotFaction(email2))//类型匹配val arr=Array("Bob",2,3.4,'d')//随机取数组元素val obj=Int(4))println(obj)obj match{case x:Int=>println(x)case s:String=>UpperCase())case d:Double =>println(Int.MaxValue)case _=>println("fail")}}
}
运行结果
you get a Email message from张三
you get a message not important
3.4
2147483647Process finished with exit code 0
you get a Email message from张三
you get a message not important
d
failProcess finished with exit code 0
本文发布于:2024-01-29 07:11:01,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170648346813583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |