想了解Java單例模式的請轉到:Java單例模式-懶漢式、惡漢式、靜態內部類、枚舉以及線程安全問題java
private
修飾/** * Desc: SCALA單例設計模式 * -懶漢式 * -餓漢式 */ object Scala16_TestSingleton { def main(args: Array[String]): Unit = { val std1: Student16 = Student16.getInstance() println(std1)//std1和std2輸出的地址同樣 val std2: Student16 = Student16.getInstance() println(std2) } } //懶漢式 object Student16{ private var s:Student16 = null def getInstance():Student16={ if(s == null){ s = new Student16 } s } } //餓漢式 object Student16{ private var s:Student16 = new Student16 def getInstance():Student16={ s } } //主構造方法私有化 class Student16 private(){}