Only simple data types and expressions can be matched in order (support: byte, short, int, char, String, enumeration, long is not supported)
Relatively speaking, the function of pattern matching math-case in Scala is much more powerful. Almost any type can be used in match and applied to more occasions.
And Scala also provides sample classes, optimized for pattern matching, and can be matched quickly.
package cn.hanjiaxiaozhi.casedemo;/**
* Author hanjiaxiaozhi
* Date 2020/7/17 11:30
* Desc 演示java中的switch
*/
public class Case_Java {
public static void main(String[] args){
//需求:根据数字打印是工作日还是休息日
int day =6;if(day <=1&& day <=5){
System.out.println("今天是工作日");}else{
System.out.println("今天是周末~休息");}//当条件很多的时候可以改造成switch
switch (day){
case1:case2:case3:case4:case5:
System.out.println("今天是工作日");
break;
default:
System.out.println("今天是周末~休息");}//Java之后的switch~case可以完成类似于if-else的条件判断//但是有限制:switch (参数),只支持:byte,short,int,String,枚举,不支持其他的复杂类型,甚至是Long//而Scala中的模式匹配很强大,支持一切~//当然Java后续的新版本也开始慢慢学习Scala}}
Pattern matching in Scala-★★★
note:
No need to use break statement in case statement
case_ is equivalent to default in Java
Only need to master the first 3, understand the others
In Scala, an object wrapper is provided for a single value, and the Option type uses a sample class to represent values that may or may not exist
The subclasses of Option are Some and None, Some wraps a certain value, and None means no value
Through the use of Option, it avoids the use of null, empty string, etc. to indicate the lack of a value
package cn.hanjiaxiaozhi.case_demo
object OptionDemo {
def main(args: Array[String]){
val map = Map("a"->1,"b"->2)//使用模式匹配:从map中根据k获取v,如果获取到了直接返回,没获取到返回默认值0val v = map.get("c")match{
//Option是一个装有1个或0个元素的容器//Some和None都是Option的子类case Some(i)=> i //Some里面有1个元素case None =>0//None里面有0个元素}
println(v)//使用getOrElseval v2:Int= map.getOrElse("a",0)
println(v2)}}
Supplement: exception handling
Description
Checked exceptions are checked at compile time. In Java, the checked exceptions that may occur in the method must be declared on the method.
The working mechanism of Scala's exceptions is the same as that of Java, but Scala does not have checked exceptions. You don't need to declare functions or methods that may throw some kind of exceptions.
Throw an exception:
Use the throw keyword to throw an exception object. All exceptions are subtypes of Throwable.
The throw expression has a type, which is Nothing. Because Nothing is a subtype of all types, the throw expression can be used where a type is needed.
Catch the exception:
In the catch code, a series of case clauses are used (borrowing the idea of pattern matching to do abnormal matching)
The mechanism of exception capture is the same as in other languages. If an exception occurs, the catch words are captured in order. Therefore, in the catch sentence, the more specific anomaly, the more advanced it is, and the more common anomaly, the more advanced it is.
If the thrown exception is not in the catch clause, the exception cannot be handled and will be escalated to the caller.
The finally word is used to perform steps that need to be performed regardless of whether it is normal processing or when an exception occurs. It is generally used for cleaning up the object.
package cn.hanjiaxiaozhi.case_demo
object ExceptionDemo {
def main(args: Array[String]):Unit={
try{
println(divider(10,0))}catch{
case ex: Exception => println("捕获了异常:"+ ex)}finally{
println("释放资源")}
println("处理了异常,代码继续往下执行")}def divider(x:Int, y:Int):Float={
if(y ==0)thrownew Exception("0作为了除数")else x / y
}}
Extension: Partial function
A set of case statements without a match enclosed in curly braces is a partial function
Partial function is an instance of PartialFunction[A, B], A represents the parameter type, B represents the return type, and is often used for input pattern matching
The biggest feature of a partial function is that it only accepts and processes a subset of its parameter domain.
package cn.hanjiaxiaozhi.case_demo
object PartialFuncDemo {
//方法def func1(num:String):Int={
num match{
case"one"=>1case"two"=>2case _ =>-1}}//偏函数val func2: PartialFunction[String,Int]={
case"one"=>1case"two"=>2case _ =>-1}def main(args: Array[String]){
//1.调用方法
println(func1("one"))//2.调用偏函数
println(func2("one"))val list = List(1,6,8)//3.传入偏函数val list2 = list.map{
case x if x >=1&& x <=5=>"工作日"case x if x >=6&& x <=7=>"休息日"case x if x >7=>"不认识"}
println(list2)}}
Extension: Extractor
We have learned before, to implement the apply method in the companion object of a class, you can use the class name to quickly build an object
There is also an unapply method in the companion object. Contrary to apply, unapply is to disassemble the object of this class into individual elements, which is often used for pattern matching
The apply method and unapply method are automatically provided in the sample class
class Student(var name:String,var age:Int)object Student {
def apply(name:String, age:Int)={
println("apply方法被调用")new Student(name, age)}def unapply(s: Student)={
println("unapply方法被调用")if(s !=null){
Some(s.name, s.age)}else{
None
}}}object TestStudent {
def main(args: Array[String]):Unit={
//调用applyval s = Student("张三",20)//调用unapply
s match{
case Student(name, age)=> println(s"${name} => ${age}")}}}