在Scala里,类型系统又比java复杂很多,泛型从一开始就存在,还支持高阶的概念。
在scala.collection.mutable.Builder中,有一个函数是这样的: def += (elem: Elem): Builder.this.type. 注意,+=的返回值是Builder.this.type,这个this.type是什么意思呢? 它和this有什么不同?
this.type表示当前对象(this)的类型。this指代当前的对象。this.type被用于变量,函数参数和函数返回值的类型声明
级联调用
对于给定的任何引用v,你可以得到它的类型v.type,它有两个可能的值:v和null。这看上去有些奇怪,但是它在某些应用场景下非常有用。
object LearnSingle1 {
class Document {
def setTitle(title: String) = {
println("The title is " + title);
this //将对象返回
}
def setAuthor(author: String) = {
println("The author is " + author);
this //将对象返回
}
}
def main(args: Array[String]) {
val article = new Document
article.setTitle("Whatever Floats Your Boat").setAuthor("Cay Horstmann") //通过返回对象的方法,我们可以做到级联调用,用一行语句调用多个函数
}
}
使用Type
this.type表示当前对象(this)的类型。this指代当前的对象。this.type被用于变量,函数参数和函数返回值的类型声明。
和c里的type有点像。scala里的类型,除了在定义class,trait,object时会产生类型,还可以通过type关键字来声明类型。
object LearnSingle3 {
class Document {
def setTitle(title: String):this.type = { //将函数的返回值改为对象的type而不是对象本身
println("The title is " + title)
this //将对象返回
}
def setAuthor(author: String):this.type = { //将函数的返回值改为对象的type而不是对象本身
println("The author is " + author)
this //将对象返回
}
}
class Book extends Document{
def addChapter(chapterName:String) = {
println("Add chapter " + chapterName) //子类新增的方法,将参数打印
}
}
def main(args: Array[String]) {
val book = new Book
book.setTitle("Whatever Floats Your Boat").setAuthor("Cay Horstmann").addChapter("Night's 7th Chapter") //继续使用级联方法调用,不会报错
}
}
那this.type
有什么作用呢?
主要是在某些场合下加强类型约束,或者说是为了确保类型的绝对安全。
将类型声明为this.type
,可以对链式调用提供安全保障。 但这在Java中是无法做到的,除非把该方法声明为final,防止被子类改写,但这样一来就失去了灵活性。Play! 的ScaleModel类便运用了Scala的这种特性。
Scala中的链式调用
链式调用,方法链(method chaining)是面向对象的编程语言中的一种常见语法,可以让开发者在只引用对象一次的情况下,对同一个对象进行多次方法调用。虽然链式调用不包含在23种设计模式,但它是一种常用的代码设计方式。
何的类对象都有type属性:this.type
class Animal {
def breathe() : this.type = {
//TODO
this
}
}
class Dog extends Animal {
def eat() : this.type = {
//TODO
this
}
}
Val dog = new Dog;
dog.breathe().eat();
应用这种写法,我们就可以在类实例化以后,进行链式的调用方法,进行方法的调用了。