抽象类定义:abstract class Element{ def contents:Array[String]; def height = contents.length}
java
定义无参数方法:若是仅提供对象的某个数据的访问(没有反作用的方法),那么久省略括号。定义成无参数方法。app
若是无参数方法用用到的地方较多而且比较频繁,建议把无参数方法def height = contents.length变成val height = 函数
contents.length的字段值。二者的区别是:由于字段值在类初始化的时候被预编译,而方法调用在每次调用的时候都须要计算,ui
另外一方面,使用字段须要为每一个Element对象分配更多空间。因此用无参数方法仍是字段值看状况而定。
spa
在scala中字段和方法属于相同的命名空间。这让字段能够重写无参数方法。scala
class WontCompile{ private var f = 0; def f = 1;}//编译无妨经过,由于字段和方法重名了。
code
java为定义准备了4个命名空间(分别是字段,方法,类,包),scala仅有2个命名空间(值(字段,方法,包,单例对象),类型(类和特质名))orm
在Scala中有一个关键字是implicit
对象
咱们先来看一个例子:ci
def display(input:String):Unit = println(input)
咱们能够看到,display
函数的定义只是接受String
类型的入参,所以调用display("any string")
这样的函数是没问题的。可是若是调用display(1)
这样的入参不是String类型的话,编译会出错的。
若是咱们想让display函数也可以支持Int类型的入参的话,除了咱们从新定一个def display(input:Int):Unit = println(input)
这样的函数之外,咱们还能够在相同的做用域内用implicit
关键字定义一个隐式转换函数,示例代码以下:
object ImplicitDemo { def display(input:String):Unit = println(input) implicit def typeConvertor(input:Int):String = input.toString implicit def typeConvertor(input:Boolean):String = if(input) "true" else "false"// implicit def booleanTypeConvertor(input:Boolean):String = if(input) "true" else "false" def main(args: Array[String]): Unit = { display("1212") display(12) display(true) } }
咱们定义了2个隐式转换函数:
implicit def typeConvertor(input:Int):String = input.toString implicit def typeConvertor(input:Boolean):String = if(input) "true" else "false"
这样display
函数就能够接受String、Int、Boolean类型的入参了。注意到上面咱们的例子中注释的那一行,若是去掉注释的那一行的话,会在运行的时候出现二义性:
Error:(18, 13) type mismatch; found : Boolean(true) required: StringNote that implicit conversions are not applicable because they are ambiguous: both method typeConvertor in object ImplicitDemo of type (input: Boolean)String and method booleanTypeConvertor in object ImplicitDemo of type (input: Boolean)String are possible conversion functions from Boolean(true) to String display(true) ^
得出的结论是:
记住隐式转换函数的同一个scop中不能存在参数和返回值彻底相同的2个implicit函数。
隐式转换函数只在乎 输入类型,返回类型。
隐式转换是scala的语法灵活和简洁的重要组成部分