单例对象
作者:
lehuai | 来源:发表于
2018-01-03 10:03 被阅读0次package day03
import scala.collection.mutable.ArrayBuffer
/**
* 单例对象
*在scala中是没有静态字段的,但是可以使用object关键字加类名的语法结构实现同样的功能
* 1、工具类,存放常量和工具方法
* 2、实现单例模式
*/
object SingletonDemo {
def main(args: Array[String]): Unit = {
val factory = SessionFactory
println(factory.getSessions)
println(factory.getSessions.size)
println(factory.getSessions(0))
println(factory.removeSession)
}
}
object SessionFactory {
/*
* 相当于java中的静态块
*/
println("SessionFactory被执行了")
var i = 5
private val session = new ArrayBuffer[Session]()
while( i > 0) {
session += new Session
i -= 1
}
def getSessions = session
def removeSession:Unit = {
val s = session(0)
session.remove(0)
println("session被移除" + s)
}
}
class Session()
本文标题:单例对象
本文链接:https://www.haomeiwen.com/subject/ckthnxtx.html
网友评论