Table of contents
The basic syntax is as follows:
Shorthand for partial functions:
Steps for implicit conversion:
Pattern matching:
Pattern matching in Scala is equivalent to switch in Java
In Java, we have the basic syntax of three components: switch case default. In Scala, we have match and case. The role of default is replaced by case.
The basic syntax is as follows:
val a=10
val b=20
var c='+'
c match {
case '+'=> println(a+b)
case '-'=> println(a-b)
case _ =>println("错误的运算符")
}
In Scala, it is executed from this case to the next case by default
case _ means none of the above conditions are satisfied
Mode Guard:
val x=3.153
val value: Any = x match {
case i: Double if i >= 0 => i
case j: Double if j < 0 => -j
case _ => "type illegal"
}
Type matches:
When defining the function, the parameter is the parent class Any of all objects
def function(x:Any): Unit ={
x match {
case s:String =>println("字符串")
case i:Int=>println("整数")
case d:Double=>println("小数")
case _ =>println("其他")
}
}
object matching
The matching of objects is more complicated. The simple comparison is to compare the address values, and the address values of the two variables must be different.
object Test_03MatchObject {
def main(args: Array[String]): Unit = {
val sss: person1 = person1("sss", 1)
sss match {
case person1("sss",1)=>println("victory")
case _=>println("defite")
}
}
class person1(val name: String,val age :Int){
}
object person1{
def apply(name: String, age: Int): person1 = new person1(name, age)
def unapply(arg: person1): Option[(String, Int)] =
if (arg==null)
{
None
}
else{
Some(arg.name,arg.age)
} }
}
Here we define the unapply method in the object. The unapply method is equivalent to the inverse application of the apply method. The apply method is to create an object in the object, and the unapply method is an extraction method to extract the object of the operation (the incoming parameter is an object, and the extracted data is stored in Some according to the object, and compared with the incoming data). This method also does not need to write the method name like the apply method.
This method is mainly used for parsing objects (matching objects)
Example class:
The main attribute in the sample class defaults to val. If you need to use var, you need to mark it yourself
The sample class will automatically generate methods such as unapply and apply, saving a lot of code
Create a new sample class after annotating the above class and object: (a lot of use)
case class person1(val name: String,val age :Int)
Partial function:
Shorthand for partial functions:
We can directly call the collect function to call the partial function parameters and directly write the case + the operation you need
The simplification method is equivalent to omitting the match, which is equivalent to filter+map
Partial functions can also be used in scenarios where maps are used
Use of partial functions:
val list = List(List(1, 2, 3, 4), List(1), List(8, 5))
val list1: List[Int] = list.collect({ case List(x, y, _*) => y })
println(list1)
Implicit conversion:
Official definition:
When the compiler fails to compile for the first time, it will search for a method that allows the code to compile in the current environment, and use
Personal understanding:
Implicit conversion is what we do when an error occurs when calling a function on an object that is not in its class
Implicit function:
Steps for implicit conversion:
(1) It is necessary to define a target class below to define the method and logic called in the class
eg: We call a non-existent method on Int type data:
class MyRichInt(val self :Int)
{
def myMax(int: Int):Int =
{
if (int>self)
int
else
self
}
}
(2) Define the converter in the main function
Introduce ---- implicit implicit conversion function at the beginning and then define it
implicit def changeInt(self:Int) =
{
new MyRichInt(self)
}
When the method of implicit conversion is the same as its own method, it will use its own method (because it will not fail to compile --- the official definition of implicit conversion)
Implicit parameters:
We already know the default value of the parameter when defining the function, but we need to add parentheses when calling
Here we introduce a new concept implicit parameter
Parameter comparison example:
implicit val name:String="lisi"
def sayhi(implicit name: String="sd"): Unit =
{
println(s"hi $name")
}
sayhi
sayhi()
The default value of the parameter is just adding a parenthesis after the calling function
operation result:
Implicit class:
Adding the keyword implicit in front of a normal class will automatically convert it to an implicit class
If the target class of the implicit conversion is converted to an implicit class, there is no need to use the new implicit class in the main function
You can directly call the specified function
That's the end of Scala! ! ! ! !