def data(array:Array[String]){
array match {
caseArray("scala")=> println("scala")
caseArray("spark",_*)=> println ("spark")
caseArray(a,b,c)=> println(a+":"+b+":"+c)
case _ =>println("else")
}
}
3.scala样例类模式匹配
相当于java中的bean
classPersoncaseclassWorker(name:String,salary:Double)extendsPersoncaseclassStudent(name:String,score:Double)extendsPersondef sayHi (person:Person){
person match{
case Student (name,score) =>println("i am student "+name +score)
case Worker (name,salary) =>println("i am worker "+name +salary)
case _ => println("unknow")
}}
sayHi(Worker("spark",6.5))
sayHi(Student("spark",6.5))
4.scala类型系统
// 相对于java中的泛型
class Person[T](val content :T){
def getContent(id:T)= id+"---"+content
}
val p = new Person[String]("spark")
p.getContent("scala")
5.scala视图界定
//支持动态类型传入
scala> class Compare [T:Ordering](val n1:T,val n2:T){
| def bigger(implicit ordered:Ordering[T])=if(ordered.compare(n1,n2)>0) n1
else n2}
defined class Compare
scala> new Compare[Int](8,3).bigger
res10: Int = 8
scala> new Compare[String]("spark","hadoop").bigger
res11: String = spark
1.scala模式匹配scala 中没有switch case 用match代替:paste可以在命令还下输入多行,最后编译执行 def bigData(data:String){ data match{ case "spark" =>println("spark") case "hadoop&quot