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, 一个则是在最后用通配符匹配.