The pattern matching scala 1, analog enumeration 2, option 3, the generic

package Scaladay02kcomkawr

1, // analog enumeration

{SimulatedEnume Object
DEF main (args: the Array [String]): Unit = {
// Case sealed to simulate enumeration class
// Define an abstract class represented by sealed modified

sealed abstract  class TrafficLight
//定义样例类来继承抽象类
case class RED(stop:String) extends TrafficLight
case  class YELLOW(waring:String) extends TrafficLight
case class GREEN(go:String) extends TrafficLight

// var tl:TrafficLight =RED("开车注意安全")

var t2: TrafficLight = GREEN ( "Please pass green light")
// define match analog enumeration
T2 match {
Case RED (STOP) => {
the println (STOP)
}
Case YELLOW (Waring) => {
the println (Waring)
}
Case GREEN (Go) => {
the println (Go)
}

} }

def method1() {

// scala defined enumerated correct format defined in the java different
class the extends the Enumeration Season {
Val SPRING, SUMMER, AUTUMN, WINTER the Value =
}
Val Season = new new Season
the println ( "Spring:" + season.SPRING )
}
}

package Scaladay02kcomkawr

2, ** // define option

// map set to get only two results and get less than **
/ **

  • If we obtain the option of direct operations, calls get no value being given regardless of value, while avoiding no value,

  • 一般都用getOrElse(default)。
    */
    object OptionMap {
    def main(args: Array[String]): Unit = {
    mthod()
    }
    def mthod(): Unit ={
    val map=Map(
    (“Amertica”->“W.D.C”),
    (“Japan”->“Tokoyo”),
    (“South Korea”->“韩城”)

    )
    // = Capital as map.get Val ( "Amertica")
    // // Option -> is a seal type sealed, meaning that all sub-classes are known, can not be added
    // {Capital match
    / / s Some Case (CC) => the println (CC)
    // Case None => the println ( "Mars")
    //}
    the println (map.getOrElse ( "India", "Mars"))
    }
    }

scaladay02.com.aura Package
// generic type parameter ==
/ **

  • scala defined using a generic [] instead as in a java with <>
    * /
    Object TypeParameters {
    DEF main (args: the Array [String]): Unit = {
    Val = new new Tool Tool Int
    // generic class is not very flexible because once the member variable is used generic top of the class so they can not use other types
    println (tool.add (3, 5))
    println (tool.add (3,5,6))

    val tool2=new Tool[String]
    println(tool2.add(“abc”,“lmn”))
    }
    }

3, generics

3.1 ** ** // generic class

class Tool[T]{
def add(a:T,b:T):T={
a
}
def add(arr:T*):T={
arr(0)

}}

package Scaladay02kcomkawr

/

3.2 * generic method

/ *
Object GenericMethod {
DEF main (args: the Array [String]): Unit = {
Val TOOL1 = new new TOOL1
tool1.show Int
tool1.show the Float
tool1.show String
}
}
{TOOL1 class
defines generic methods // Note pan type should be written after the method name with [] enclosed
DEF Show T {
the println (arr.mkString ( ","))

}
}
Use the generic can not pay attention to member variables can only be used on a custom generic class definition

package Scaladay02kcomkawr

3.3 ** @ defining generic views defined and

/****

  • To an upper limit defined in the scala syntax structure [T <: xxx] If wild cards can be used?

  • Used to define the lower limit of the defined syntax structure [T>: xxx] In scala in If wild cards may be used _
    /
    Object Definition {
    DEF main (args: the Array [String]): Unit = {
    Val ARR = the Array Int
    Val strArr the array = String
    the println ( "pre-sorted array:" + arr.mkString ( ","))
    SelectSort (ARR)
    the println ( "sorted array:" + arr.mkString ( ","))
    }
    // custom sorting in a way to make the scala type generally includes a comparative with the Comparable [T]
    // make a specific set in the scala generally used in Comparative comparator [T]
    /
    *

    • Int because no progressive expansion Comparable interface so we can not use direct xx.comparaTo operation
    • But we can [T <: Comparable [T]] changed [T <% Comparable [T]] The compiler can be passed
    • At this time, since the bottom has been implicit conversion

    */
    def selectSortT<% Comparable[T]{
    for (a <- 0 to arr.length){
    for (b <-a until arr.length){
    if (arr(a).compareTo(arr(b))>0){
    swap(arr,a,b)

     }
    

    }
    }
    }
    def swapT{
    var temp=arr(a)
    arr(a)=arr(b)
    arr(b)=temp

}
}

Guess you like

Origin blog.csdn.net/weixin_44701192/article/details/92070007