scala包引用 _root_ 表示頂包簡化引用.(如:com.zd.test寫成_root_.test)java
scala包引用能夠放在任意做用域位置.如:編程
def test(){ import _root_.Thread }
引用選擇器能夠包括下列模式:app
簡單名X.把X包包含進引用名集.函數
重命名子句x => y.讓名爲x的成員以名稱y出現.this
隱藏子句x => _.把x排除在引用名集以外.spa
全包括 '_'.引用出了前面字句提到的以外的全體成員.若是存在全包括,那麼必須是引用選擇的最後一個.scala
若是有相同的包或類靠後的引用將覆蓋靠前的引用.3d
訪問修飾符:code
私有成員:private與java同樣.對象
保護成員:protected與java有點差別,僅有繼承的類或特質能夠訪問.同包下不可訪問.
公開成員:沒有任何修飾符的就是公開成員與java一直.
scala保護的做用域:
能夠限定的更明確,格式爲private[X]或protected[X]的修飾符表示"直到"X的私有或保護,這裏X指代某個所屬的包,類或單例對象.
伴生類和伴生對象能夠相互訪問private修飾的成員.
在Scala中一共有以下幾種類型的模式匹配:
通配符匹配(Wildcard Pattern Matching )
常量匹配 (Constant Pattern Matching )
變量匹配(Variable Pattern Matching )
構造函數匹配(Constructor Pattern Matching )
集合類型匹配(Sequence Pattern Matching )
元祖類型匹配(Tuple Pattern Matching )
類型匹配(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")) } }
執行結果:
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
關鍵字主要有2個做用:
若是有遺漏有三個方式能夠解決這個問題,一個是加上遺漏case class XX的處理,一個是使用unchecked annotation, 一個則是在最後用通配符匹配.