泛型: Context Bounds
// //定义一个隐式值, 这个值不能少, 要不找不到比较的对象 implicit val personCompartor = new Ordering[Person] { override def compare(p1: Person, p2: Person): Int = { p1.age - p2.age } } def main(args: Array[String]): Unit = { val jack = new Person("jack", 20) val tom = new Person("tom", 30) val compareComm1 = new CompareComm(jack, tom) println(compareComm1.greater) println("---------------------") val compareComm2 = new CompareComm2(jack, tom) println(compareComm2.geatter) println("--------------------------") val compareComm3 = new CompareComm3(jack, tom) println(compareComm3.greatter) } } // 一个普通漫的personl类 class Person(val name: String, val age: Int) { override def toString: String = this.name + " " + this.age } /** 方式1 * implicit comparetor: Ordering[T] 是一个隐式参数 * 前面Ordered , Ordering * Ordered和Ordering的区别 * Ordering继承了java中的Comparator接口,而Ordered继承了java的Comparable接口。 * 在java中的Comparator是一个外部比较器(需要定义一个类来实现比较器),而Comparable则是一个内部比较器,在类内部重载compareTo函数。 * */ class CompareComm[T: Ordering](obj1: T, obj2: T)(implicit comparetor: Ordering[T]) { def greater = if (comparetor.compare(obj1, obj2) > 0) obj1 else obj2 } /** * 方式二 * 隐式参数也可以直接写到方法中 */ class CompareComm2[T: Ordering](obj1: T, obj2: T) { def geatter = { def f1(implicit comparetor: Ordering[T]) = comparetor.compare(obj1, obj2) if (f1 > 0) obj1 else obj2 } } /** * 方式3,使用implicitly语法糖,最简单(推荐使用) */ class CompareComm3[T: Ordering](obj1: T, obj2: T) { def greatter = { //发生隐式转换,获取到隐式值 personComparetor val comparetor = implicitly[Ordering[T]] if (comparetor.compare(obj1, obj2) > 0) obj1 else obj2 } }
模式匹配日志:
//标准模式匹配, 可以在匹配中读取变量名称, 模式中也可是设置守卫, 可以变量赋值_newVa def judgeGrade(name: String, grade: String) { grade match { case "A" => println(name + ", you are excellent") case "B" => println(name + ", you are good") case "C" => println(name + ", you are just so so") case _newValu if name == "leo" => println(name + ", you are a good boy, come on ,_newValu: " + _newValu) case _ => println("you need to work harder") } } judgeGrade("leo", "D") //类型模式匹配 def test(e: Exception): Unit = { e match { case e1: IllegalArgumentException => println("IllegualArgument") case e2: FileNotFoundException => println("FileNotFoundExceptino") case e3: NullPointerException => println("NullPointer") case _: Exception => println("common Exception") } } //对 Array 进行模式匹配 def greeting(arr: Array[String]) { arr match { case Array("Leo") => println("Hi, Leo!") case Array(girl1, girl2, girl3) => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3) case Array("Leo", _*) => println("Hi, Leo, please introduce your friends to me.") case _ => println("hey, who are you?") } } //对 List 进行模式匹配 def greeting(list: List[String]) { list match { case "Leo" :: Nil => println("Hi, Leo!") //一个元素, 后面是 nil case girl1 :: girl2 :: girl3 :: Nil => println("Hi, girls, nice to meet you. " + girl1 + " and " + girl2 + " and " + girl3) case "Leo" :: tail => println("Hi, Leo, please introduce your friends to me.") case _ => println("hey, who are you?") } } //case Class 模式匹配 class Person case class Teacher(name: String, subject: String) extends Person case class Student(name: String, classroom: String) extends Person def judgeIdentify(p: Person) { p match { case Teacher(name, subject) => println("Teacher, name is " + name + ", subject is " + subject) case Student(name, classroom) => println("Student, name is " + name + ", classroom is " + classroom) case _ => println("Illegal access, please go out of the school!") } } //Option 模式匹配 val grades = Map("Leo" -> "A", "Jack" -> "B", "Jen" -> "C") def getGrade(name: String) { val grade = grades.get(name) grade match { case Some(grade) => println("your grade is " + grade) case None => println("Sorry, your grade information is not in the system") } }