scala_with关键字

with 关键字

with关键字可以用来实现包装器的功能,新建withSample.scala文件示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
trait Friendly {
def greet() = "Hi"
}

class Dog extends Friendly {
override def greet() = "Woof"
}

class HungryDog extends Dog {
override def greet() = "I'd like to eat my own dog food"
}

trait ExclamatoryGreeter extends Friendly {
override def greet() = super.greet() + "!"
}

var pet: Friendly = new Dog
println(pet.greet())

pet = new HungryDog
println(pet.greet())

pet = new Dog with ExclamatoryGreeter
println(pet.greet())

pet = new HungryDog with ExclamatoryGreeter
println(pet.greet())

用scala命令调用(scala withSample.scala),输出如下

1
2
3
4
woof
I'd like to eat my own dog food
woof!
I'd like to eat my own dog food!

张逸的Scala中的Partial Function一文中提到PartialFunction的定义如下:

1
2
3
4
5
6
trait PartialFunction[-A, +B] extends (A => B) { self =>
import PartialFunction._
def isDefinedAt(x: A): Boolean
def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 => B1): B1 =
if (isDefinedAt(x)) apply(x) else default(x)
}

追本溯源,是因为这里对偏函数值的调用,实则是调用了AbstractPartialFunction的apply()方法(case语句相当于是继承AbstractPartialFunction的子类):

1
2
3
	abstract class AbstractPartialFunction[@specialized(scala.Int, scala.Long, scala.Float, scala.Double, scala.AnyRef) -T1, @specialized(scala.Unit, scala.Boolean, scala.Int, scala.Float, scala.Long, scala.Double, scala.AnyRef) +R] extends Function1[T1, R] with PartialFunction[T1, R] { self =>
def apply(x: T1): R = applyOrElse(x, PartialFunction.empty)
}

看到里面用到了with PartialFunction[T1,R]。

Scala 学习笔记(一)

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%