Scala learning (6)

The scala package reference _root_ represents the top package simplified reference. (For example: com.zd.test is written as _root_.test )

Scala package references can be placed in any scoped location. For example:

def test(){
  import _root_.Thread
}

Reference selectors can include the following patterns:

Simple name X. Include the X package into the reference name set.

Rename clause x => y. Let the member named x appear with the name y.

The hidden clause x => _. excludes x from the set of reference names.

All-inclusive '_'. All members other than those mentioned in the previous sentence are quoted. If there is an all-inclusive, it must be the last one selected by the quotation.

If there is the same package or class later reference will override earlier reference.

Access modifier:

Private members: private is the same as java.

Protected members: protected is a bit different from java, only inherited classes or traits can be accessed. It is not accessible under the same package.

Public members: Without any modifiers, public members are the same as java.

Scala protected scopes:

To be more specific, modifiers of the form private [X] or protected [X] denote private or protected "up to" X , where X refers to an owning package, class, or singleton object .

Companion classes and companion objects can access privately decorated members of each other.

There are the following types of pattern matching in Scala:

  1. Wildcard Pattern Matching

  2. Constant Pattern Matching

  3. Variable Pattern Matching

  4. Constructor Pattern Matching

  5. Sequence Pattern Matching

  6. Tuple Pattern Matching

  7. Typed Pattern Matching

object PatternMatchingDemo {

    case class Person(firstName: String, lastName: String)
    case class Dog(name: String)

    def echoWhatYouGaveMe(x: Any): String = x match {
        // constant patterns
        case 0 => "zero"
        case true => "true"
        case "hello" => "you said 'hello'"
        case Nil => "an empty List"
        // sequence patterns
        case List(0, _, _) => "a three-element list with 0 as the first element"
        case List(1, _*) => "a list beginning with 1, having any number of elements"
        case Vector(1, _*) => "a vector starting with 1, having any number of elements"
        // tuples
        case (a, b) => s"got $a and $b"
        case (a, b, c) => s"got $a, $b, and $c"
        // constructor patterns
        case Person(first, "Alexander") => s"found an Alexander, first name = $first"
        case Dog("Suka") => "found a dog named Suka"
        // typed patterns
        case s: String => s"you gave me this string: $s"
        case i: Int => s"thanks for the int: $i"
        case f: Float => s"thanks for the float: $f"
        case a: Array[Int] => s"an array of int: ${a.mkString(",")}"
        case as: Array[String] => s"an array of strings: ${as.mkString(",")}"
        case d: Dog => s"dog: ${d.name}"
        case list: List[_] => s"thanks for the List: $list"
        case m: Map[_, _] => m.toString
        // the default wildcard pattern
        case _ => "Unknown"
    }

    def main(args: Array[String]) {
        // trigger the constant patterns
        println(echoWhatYouGaveMe(0))
        println(echoWhatYouGaveMe(true))
        println(echoWhatYouGaveMe("hello"))
        println(echoWhatYouGaveMe(Nil))
        // trigger the sequence patterns
        println(echoWhatYouGaveMe(List(0,1,2)))
        println(echoWhatYouGaveMe(List(1,2)))
        println(echoWhatYouGaveMe(List(1,2,3)))
        println(echoWhatYouGaveMe(Vector(1,2,3)))
        // trigger the tuple patterns
        println(echoWhatYouGaveMe((1,2))) // two element tuple
        println(echoWhatYouGaveMe((1,2,3))) // three element tuple
        // trigger the constructor patterns
        println(echoWhatYouGaveMe(Person("Melissa", "Alexander")))
        println(echoWhatYouGaveMe(Dog("Suka")))
        // trigger the typed patterns
        println(echoWhatYouGaveMe("Hello, world"))
        println(echoWhatYouGaveMe(42))
        println(echoWhatYouGaveMe(42F))
        println(echoWhatYouGaveMe(Array(1,2,3)))
        println(echoWhatYouGaveMe(Array("coffee", "apple pie")))
        println(echoWhatYouGaveMe(Dog("Fido")))
        println(echoWhatYouGaveMe(List("apple", "banana")))
        println(echoWhatYouGaveMe(Map(1->"Al", 2->"Alexander")))
        // trigger the wildcard pattern
        println(echoWhatYouGaveMe("33d"))
    }
}

Results of the: 

zero
true
you said 'hello'
an empty List
a three-element list with 0 as the first element
a list beginning with 1, having any number of elements
a list beginning with 1, having any number of elements
a vector starting with 1, having any number of elements
got 1 and 2
got 1, 2, and 3
found an Alexander, first name = Melissa
found a dog named Suka
you gave me this string: Hello, world
thanks for the int: 42
thanks for the float: 42.0
an array of int: 1,2,3
an array of strings: coffee,apple pie
dog: Fido
thanks for the List: List(apple, banana)
Map(1 -> Al, 2 -> Alexander)
you gave me this string: 33d

sealed

From the above description, we can know that sealed keywords have two main functions:

  • Its modified trait, class can only be inherited in the current file
  • The purpose of decorating with sealed is to tell the scala compiler to let scala know all the cases of these cases when checking for pattern matching, and scala can check at compile time to see if the code you wrote has missed anything. case to reduce programming errors.

 If there are omissions, there are three ways to solve this problem, one is to add the processing of missing case class XX, one is to use unchecked annotation, and the other is to use wildcard matching at the end.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813930&siteId=291194637