1. 隐式转换
- 隐式转换是scala中一种特殊的功能,能在不改动已有class设计的情况下为class添加新的方法
- 隐式转换是把一种类型安全地转成另一种类型,原数据类型将拥有新的数据类型的所有方法,也可以看成是对类的一种增强
- 定义隐式转换的关键字是 implicit
- 隐式转换的常用命名方式是 one2one
- 隐私转换分为两种:
- 隐式转换函数
- 隐式转换值
1.1 隐式转换函数
class ImplicitDemo1_1(var name: String)
class ImplicitDemo1_2(var name: String)
class ImplicitDemo1_3(var name: String)
object ImplicitDemo1_4{
implicit def any2ImplicitDemo1_1(any: Any) = {
if (any.isInstanceOf[ImplicitDemo1_2]) {
val i2 = any.asInstanceOf[ImplicitDemo1_2]
new ImplicitDemo1_1(i2.name)
} else if (any.isInstanceOf[ImplicitDemo1_3]) {
val i3 = any.asInstanceOf[ImplicitDemo1_3]
new ImplicitDemo1_1(i3.name)
} else {
null
}
}
}
object ImplicitDemo1 {
def whichClass(p: ImplicitDemo1_1) = {
if (p != null) {
println("is ImplicitDemo1_1:" + p.name)
} else {
println("no ImplicitDemo1_1:")
}
}
def main(args: Array[String]): Unit = {
val i1 = new ImplicitDemo1_1("xiaom")
val i2 = new ImplicitDemo1_2("xioah")
val i3 = new ImplicitDemo1_3("xioag")
whichClass(i1)
import ImplicitDemo1_4.any2ImplicitDemo1_1
whichClass(i2)
whichClass(i3)
}
}
1.2 隐式参数
class ImplicitDemo2_1(var name:String){
def write(content:String)= println(s"name=$name, content=$content")
}
object ImplicitDemo2 {
def singForExam(content:String)(implicit i1:ImplicitDemo2_1)={
i1.write(content)
}
def main(args: Array[String]): Unit = {
implicit val i1 = new ImplicitDemo2_1("你好")
singForExam("小明")
}
}
1.3 隐式类型转换
object ImplicitDemo3 {
implicit def foo(s:String):Int = Integer.parseInt(s)
def add(a:Int, b:Int) = a+b
def main(args: Array[String]): Unit = {
println(add("100",8))
}
}
1.4 隐式为Int增加next 与求阶乘方法
object ImplicitDemo3_1 {
implicit def next(n: Int) = new {
def next = n + 1
}
def pow(n: Int, m: Int): Int = if (m == 0) 1 else n * pow(n, m - 1)
implicit def foo(n: Int) = new {
def **(m: Int) = pow(n, m)
}
def main(args: Array[String]): Unit = {
println(10.next)
println(2**10)
}
}
1.5 隐式对类的增强
object ImplicitDemo4 {
implicit def file2RichFile(file:File):RichFile = new RichFile(file)
def main(args: Array[String]): Unit = {
val file = new File("/opt/apache-maven-3.6.2/conf/settings.xml")
file.read()
}
}
class RichFile(var file:File){
def read()={
val content = Source.fromFile(file).mkString
println(content)
}
}
2. 视图界定
- 视图界定用 <% 来表示 (T <% Class1),表示 T 只能是 Class1 的子类或者 T 能隐式转换成 Class1 类,
- 视图界定是对泛型中类的上边界的一种增强
class Human(var name:String){
def sayHello(): Unit ={
println(s"hello, I'm $name")
}
def makeFriends(p:Human)={
sayHello()
p.sayHello()
}
}
class Student(name:String) extends Human(name)
class Cat(var name:String){
def sayHello(): Unit ={
println(s"I'm a cat name is $name")
}
}
class Party [ T<% Human](p1:T,p2:T){
p1.makeFriends(p2)
}
object ImplicitDemo05 {
def main(args: Array[String]): Unit = {
val human1 = new Human("小明")
val human2 = new Human("小红")
val party1 = new Party[Human](human1,human2)
val s1 = new Student("小学生")
val s2 = new Student("中学生")
val party2 = new Party[Student](s1,s2)
val c1 = new Cat("加菲猫")
val c2 = new Cat("小花猫")
implicit def cat2Human(cat:Cat):Human ={
new Human(cat.name)
}
val party3 = new Party[Cat](c1,c2)
}
}