一、对象
1、object
object,相当于class的单个实例,通常在里面放一些静态的field或者method,第一次调用object的方法时,就会执行object的constructor, 也就是object内部不在method中的代码,但是object不能定义接受参数的constructor。 object的constructor只会在其第一次被调用时执行一次, 以后再次调用就不会再次执行constructor了。 object通常用于作为单例模式的实现,或者放class的静态成员,比如工具方法。 ### scala> :paste // Entering paste mode (ctrl-D to finish) object Person { private var eyeNum = 2 println("this is person object!") def getEyeNum = eyeNum } // Exiting paste mode, now interpreting. defined object Person scala> Person.eyeNum <console>:12: error: variable eyeNum in object Person cannot be accessed in object Person Person.eyeNum ^ scala> Person.getEyeNum this is person object! res1: Int = 2 scala> Person.getEyeNum #object里的代码只会执行一次,再次调用就不会执行了 res2: Int = 2
2、伴生对象
如果有一个class,还有一个与class同名的object,那么就称这个object是class的伴生对象,class是object的伴生类。
伴生类和伴生对象必须存放在一个.scala文件之中,伴生类和伴生对象,最大的特点在于,互相可以访问private field。
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person(val name: String, val age: Int) {
def sayHello = println("Hi, " + name + ", I know you are " + age + "years old" + ", and usually you must have " + Person.eyeNum + " eyes.")
}
object Person {
private val eyeNum = 2
def getEyeNum = eyeNum
}
// Exiting paste mode, now interpreting.
defined class Person
defined object Person
scala> val p = new Person("leo", 30)
p: Person = Person@21588809
scala> p.sayHello
Hi, leo, I know you are 30years old, and usually you must have 2 eyes.
3、object继承抽象类
object的功能和class类似,除了不能定义接收参数的constructor之外,object也可以继承抽象类,并覆盖抽象类中的方法。
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
abstract class Hello(var message: String) {
def sayHello(name: String): Unit
}
object HelloImpl extends Hello("hello") {
override def sayHello(name: String) = {
println(message + "," + name)
}
}
// Exiting paste mode, now interpreting.
defined class Hello
defined object HelloImpl
scala> HelloImpl.sayHello("leo")
hello,leo
4、apply方法
object中重要的一个特殊方法,apply方法,通常在伴生对象中实现apply方法,并在其中实现构造伴生类的对象的功能。
而创建伴生类的对象时,通常不会使用new Class的方式,而是使用Class()的方式,隐式调用伴生对象的apply方法,让对象创建更简洁。
如Array类的伴生对象的apply方法就实现了接收可变数量的参数,并创建一个Array对象的功能。
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
###
scala> :paste
// Entering paste mode (ctrl-D to finish)
class Person(val name: String)
object Person {
def apply(name: String) = new Person(name)
}
// Exiting paste mode, now interpreting.
defined class Person
defined object Person
scala> val p1 = new Person("leo")
p1: Person = Person@6bf08014
scala> p1.name
res2: String = leo
scala> val p2 = Person("leo")
p2: Person = Person@2c07545f
scala> p2.name
res3: String = leo
5、main方法
在Scala中,main方法作为应用程序的入口,Scala中的main方法定义为def main(args:Array[String]),而且必须定义在object中。
###
[root@saltstack scala]# vim HelloWord.scala
object HelloWord {
def main(args: Array[String]) {
println("Hello Word!")
}
}
[root@saltstack scala]# scalac HelloWord.scala
[root@saltstack scala]# ls
HelloWord.class HelloWord$.class HelloWord.scala
[root@saltstack scala]# scala HelloWord
Hello Word!
除了自己实现main方法,还可以继承App Train,然后将需要在main方法中运行的代码,直接作为object的constructor代码,而且用args可以接受传入的参数。
###
[root@saltstack scala]# vim HelloWord.scala
object HelloWord extends App {
if (args.length > 0) println("Hello, " + args(0))
else println("Hello Word!")
}
[root@saltstack scala]# scalac HelloWord.scala
[root@saltstack scala]# scala HelloWord
Hello Word!
[root@saltstack scala]# scala HelloWord leo
Hello, leo
6、用object来实现枚举功能
Scala没有直接提供类似于Java的Enum枚举特性,如果要实现枚举,则需要用object继承Enumeration,并且调用Value方法来初始化枚举值。 ### scala> :paste // Entering paste mode (ctrl-D to finish) object Season extends Enumeration { val SPRING, SUMMER, AUTUMN, WINTER = Value } // Exiting paste mode, now interpreting. defined object Season scala> Season.SPRING res4: Season.Value = SPRING 还可以通过Value传入枚举值的id和name,通过id和toString可以获取,还可以通过id和name来查找枚举值。 ### scala> :paste // Entering paste mode (ctrl-D to finish) object Season extends Enumeration { val SPRING = Value(0, "spring") val SUMMER = Value(1, "summer") val AUTUMN = Value(2, "autumn") val WINTER = Value(3, "winter") } // Exiting paste mode, now interpreting. defined object Season scala> Season.SPRING.id res5: Int = 0 scala> Season.SPRING.toString res6: String = spring scala> Season(0) res7: Season.Value = spring scala> Season(1) res8: Season.Value = summer scala> Season.withName("winter") res9: Season.Value = winter scala> for (ele <- Season.values) println(ele) ##遍历 spring summer autumn winter