Scala 入门笔记-单例对象
在Scala中实现静态字段和静态方法的方式是用Object关键字来实现
1.工具类,存放常量和工具方法
2.实现单例模式
package day03
import scala.collection.mutable.ArrayBuffer
object SingletonDemo {
def main(args: Array[String]): Unit = {
val sessionFactory = SessionFactory
println(sessionFactory.getSession)
println(sessionFactory.getSession.size)
println(sessionFactory.removeSession)
}
}
object SessionFactory {
println("SessionFactory")
var i = 5
private val session = new ArrayBuffer[Session]
while (i > 0) {
session += new Session
i -= 1
}
def getSession = session
def removeSession : Unit = {
session.remove(0)
println("session " + session(0) + " remove")
}
}
class Session {}

浙公网安备 33010602011771号