学校做好网站建设目的,女排联赛最新排行榜,织梦网站怎么做301,推广软件是什么意思目录 异常
隐式转换
隐式函数
隐式参数
隐式类
隐式解析机制
泛型
泛型上下限
上下文限定
来源#xff1a; 异常 def main(args: Array[String]): Unit {try {var n 10 / 0}catch {case ex: ArithmeticException{// 发生算术异常println(发生算术异常 异常 def main(args: Array[String]): Unit {try {var n 10 / 0}catch {case ex: ArithmeticException{// 发生算术异常println(发生算术异常)}case ex: Exception{// 对异常处理println(发生了异常 1)println(发生了异常 2)}}finally {println(finally)}
} 1我们将可疑代码封装在 try 块中。在 try 块之后使用了一个 catch 处理程序来捕获异 常。如果发生任何异常catch 处理程序将处理它程序将不会异常终止。 2Scala 的异常的工作机制和 Java 一样但是 Scala 没有“checked编译期”异常 即 Scala 没有编译异常这个概念异常都是在运行的时候捕获处理。 3异常捕捉的机制与其他语言中一样如果有异常发生catch 子句是按次序捕捉的。 因此在 catch 子句中越具体的异常越要靠前越普遍的异常越靠后如果把越普遍的异 常写在前把具体的异常写在后在 Scala 中也不会报错但这样是非常不好的编程风格。 4finally 子句用于执行不管是正常处理还是有异常发生时都需要执行的步骤一般用 于对象的清理工作这点和 Java 一样。 5用 throw 关键字抛出一个异常对象。所有异常都是 Throwable 的子类型。throw 表 达式是有类型的就是 Nothing因为 Nothing 是所有类型的子类型所以 throw 表达式可 以用在需要类型的地方 def test():Nothing {throw new Exception(不对)
} 6java 提供了 throws 关键字来声明异常。可以使用方法定义声明异常。它向调用者函 数提供了此方法可能引发此异常的信息。它有助于调用函数处理并将该代码包含在 try-catch 块中以避免程序异常终止。在 Scala 中可以使用 throws 注解来声明异常 def main(args: Array[String]): Unit {f11()
}
throws(classOf[NumberFormatException])
def f11(){abc.toInt
} 隐式转换 当编译器第一次编译失败的时候会在当前的环境中查找能让代码编译通过的方法用 于将类型进行转换实现二次编译 可以扩展代码的功能。相当于设置了默认值。 隐式转换可以在不需改任何代码的情况下扩展某个类的功能 隐式函数 class MyRichInt(val self: Int) {def myMax(i: Int): Int {if (self i) i else self}def myMin(i: Int): Int {if (self i) self else i}
}
object TestImplicitFunction {// 使用 implicit 关键字声明的函数称之为隐式函数
implicit def convert(arg: Int): MyRichInt {new MyRichInt(arg)}
def main(args: Array[String]): Unit {// 当想调用对象功能时如果编译错误那么编译器会尝试在当前作用域范
围内查找能调用对应功能的转换规则这个调用过程是由编译器完成的所以称之为隐
式转换。也称之为自动转换println(2.myMax(6))}
} 隐式参数 普通方法或者函数中的参数可以通过 implicit 关键字声明为隐式参数调用该方法时 就可以传入该参数编译器会在相应的作用域寻找符合条件的隐式值。 1同一个作用域中相同类型的隐式值只能有一个 2编译器按照隐式参数的类型去寻找对应类型的隐式值与隐式值的名称无关。 3隐式参数优先于默认参数 object TestImplicitParameter {implicit val str: String hello world!def hello(implicit arg: Stringgood bey world!): Unit {println(arg)}def main(args: Array[String]): Unit {hello}
} 隐式类 在 Scala2.10 后提供了隐式类可以使用 implicit 声明类隐式类的非常强大同样可 以扩展类的功能在集合中隐式类会发挥重要的作用。 1其所带的构造参数有且只能有一个 2隐式类必须被定义在“类”或“伴生对象”或“包对象”里即隐式类不能是顶 级的 object TestImplicitClass {implicit class MyRichInt(arg: Int) {def myMax(i: Int): Int {if (arg i) i else arg}def myMin(i: Int) {if (arg i) arg else i}}def main(args: Array[String]): Unit {println(1.myMax(3))}
} 隐式解析机制 1首先会在当前代码作用域下查找隐式实体隐式方法、隐式类、隐式对象。一 般是这种情况 2如果第一条规则查找隐式实体失败会继续在隐式参数的类型的作用域里查找。 类型的作用域是指与该类型相关联的全部伴生对象以及该类型所在包的包对象。 package com.atguigu.chapter10
import com.atguigu.chapter10.Scala05_Transform4.Teacher
//2如果第一条规则查找隐式实体失败会继续在隐式参数的类型的作用域里查找。
类型的作用域是指与该类型相关联的全部伴生模块
object TestTransform extends PersonTrait {def main(args: Array[String]): Unit {
//1首先会在当前代码作用域下查找隐式实体val teacher new Teacher()teacher.eat()teacher.say()}class Teacher {def eat(): Unit {println(eat...)}}
}
trait PersonTrait {
}
object PersonTrait {// 隐式类 : 类型 1 类型 2implicit class Person5(user:Teacher) {def say(): Unit {println(say...)}}
} 泛型 1语法 class MyList[T]{ //协变 } class MyList[-T]{ //逆变 } class MyList[T] //不变 协变Son 是 Father 的子类则 MyList[Son] 也作为 MyList[Father]的“子类”。 逆变Son 是 Father 的子类则 MyList[Son]作为 MyList[Father]的“父类”。 不变Son 是 Father 的子类则 MyList[Father]与 MyList[Son]“无父子关系”。 //泛型模板
//class MyListT{}
//不变
//class MyList[T]{}
//协变
//class MyList[T]{}
//逆变
//class MyList[-T]{}
class Parent{}
class Child extends Parent{}
class SubChild extends Child{}
object Scala_TestGeneric {def main(args: Array[String]): Unit {//var s:MyList[Child] new MyList[SubChild]}
} 泛型上下限 Class PersonList[T : Person]{ //泛型上限 } Class PersonList[T : Person]{ //泛型下限 } class Parent{}
class Child extends Parent{}
class SubChild extends Child{}
object Scala_TestGeneric {def main(args: Array[String]): Unit {//test(classOf[SubChild])//test[Child](new SubChild)}//泛型通配符之上限//def test[A : Child](a:Class[A]): Unit {// println(a)//}//泛型通配符之下限//def test[A : Child](a:Class[A]): Unit {// println(a)//}//泛型通配符之下限 形式扩展def test[A : Child](a:A): Unit {println(a.getClass.getName)}
}上下文限定 def f[A : B](a: A) println(a) //等同于 def f[A](a:A)(implicit arg:B[A])println(a) 上下文限定是将泛型和隐式转换的结合产物以下两者功能相同使用上下文限定[A : Ordering]之后方法内无法使用隐式参数名调用隐式参数需要通过 implicitly[Ordering[A]] 获取隐式变量如果此时无法查找到对应类型的隐式变量会发生出错误 def f[A:Ordering](a:A,b:A) implicitly[Ordering[A]].compare(a,b)
def f[A](a: A, b: A)(implicit ord: Ordering[A]) ord.compare(a, b) 来源 尚硅谷