Scala Trait+Match+PartialFunction+CaseClass

1,Trait
When a class inherits trait, the first keyword uses extends, followed by with

trait Read{
  def read(name:String) {
    println(s"$name is reading.....")
  }
}
  trait Listen {
    def listen(name: String) {
      println(s"$name is listening.....")
    }
  }
class  Person() extends Read with Listen {
}
object Demo9_Trait1 {
  def main(args: Array[String]): Unit = {
  val p = new Person()
    p.read("zhangsan")
    p.listen("lisi")
  }
}

It can be implemented in method body or unimplemented in method body, and unimplemented in subclass

trait IsEquale{
  def isEqu(o:Any):Boolean
  def isNotEqu(o:Any):Boolean = !isEqu(o)
}
class Point(xx:Int,xy:Int)extends IsEquale {
  val x = xx
  val y = xy
  override def isEqu(o:Any):Boolean={
    o.isInstanceOf[Point]&&o.asInstanceOf[Point].x == this.x
  }
}
object Demo10_Trait2 {
  def main(args: Array[String]): Unit = {
    val p1 = new Point(xx=1,xy=2)
    val p2 = new Point(xx=1,xy=3)
    println(p1.isEqu(p2))
    println(p1.isNotEqu(p2))
  }
}

2. Match pattern matching

1) case "doesn't match anything, put it at the end
2) match can match values and types
3) there will be value conversion during matching
4) matching from online to offline will automatically terminate
5) the brackets outside the pattern match can be omitted

object Demo11_Match {
  def main(args: Array[String]): Unit = {
    val tp = (1,1.2,"abc",'a',true)
    val iter = tp.productIterator
    iter.foreach(MatchTest)
  }
  def MatchTest(o:Any): Unit ={
    o match {
      case 1=>println("value is 1")
      case i=>println(s"type is Int, value is $i")
      case d=>println(s"type is Double, value is $d")
      case s=>println(s"type is String, value is $s")
      case 'a'=>println("value is a")
      case _=>{println("no match...")}
    }
  }
}

3. Partial function

Partial function, only one value can be matched, and a value B will be returned for matching
PartialFunction[A, B] A is the matching type, B is the return type on matching

object Demo12_PartialFun {
  def MyTest:PartialFunction[String,Int]={
    case "abc"=>2
    case  "a"=>1
    case _=>200

  }
  def main(args: Array[String]): Unit = {
    val result = MyTest("abc")
    println(result)
    println(MyTest("abcd"))
  }
}

4. Example class CaseClass

/**
  * Sample class
  * 1,The class defined by case is the sample class,
  * It implements the getter method of class construction parameters (the default construction is declared as val),
  * When the construction parameter is var, it will help you implement setter and getter methods
  * 2,The sample class implements toString, equals by default. copy and hashCode etc
  * 3,The sample class can be new or not new
  */
case class Person1(name:String,age:Int){

}
object Demo13_CaseClass {
  def main(args: Array[String]): Unit = {
    val p1 = new Person1("zhangsan",19)
    val p2 = new Person1("zhangsan",19)
    println(p1.equals(p2))
    println(p1)
  }
}

Posted by Karlos94 on Tue, 26 Nov 2019 06:54:48 -0800