单例:
代码语言:javascript
复制object singleton {
/**
* 利用单例object:
* 作为存放工具函数或者常量
* 高效共享单个不可变实例
* 单例模式协调
*/
object ID {
private var lastNumber = 0;
def newUniqueId = {
lastNumber = 1
lastNumber
}
}
}
伴生
代码语言:javascript
复制object associate {
/**
* 伴生对象
* 针对又有静态方法,又有实例方法的
*/
object Account {
private var lastNumber = 0;
def newUniqueId = {
lastNumber = 1
lastNumber
}
}
class Account {
var balance = 0.0
def deposit(value:Double) = {
balance = value
balance
}
def withdraw(value:Double) = {
balance -= value
balance
}
}
}
apply方法:
代码语言:javascript
复制object apply {
class Account private(val id: Int, private var balance: Double) {
def deposit(value: Double) = {
balance = value
balance
}
def withdraw(value: Double) = {
balance -= value
balance
}
def currentBalance = balance
}
object Account {
var Id = 0
def getId = {
Id = 1
Id
}
/**
* 定义apply方法,在外部可以调用
* val a = Account(99)
* println(a.id)//输出:1
* println(a.currentBalance)//输出:99.0
* @param balance
* @return
*/
def apply(balance: Double) = {
new Account(getId, balance)
}
}
}
包:
代码语言:javascript
复制package com {
package hash {
package learn {
object test1 {
def execute = {
println("包的文件不一定要对应的文件夹下")
}
}
package scala {
object test2 {
def execute = {
println("同一文件可以包含多个包")
}
}
object Utils {
def execute = {
println("作用域支持嵌套,可以访问上层名称")
}
}
package Chapter7 {
object test3 {
def execute = {
Utils.execute //作用域支持嵌套,可以访问上层名称
}
}
}
}
}
}
}
package com {
package hash {
package collection {
}
package test {
object test4 {
def execute = {
//默认scala包是被载入的
//val a = collection.mutable.ArrayBuffer(1,2,3,4) 语句有错,因为是相对路径引入包
//任意地方可以import可以:
import _root_.scala.collection.mutable.ArrayBuffer
val a = ArrayBuffer(1,2,3,4)
//也可以绝对路径
val b = _root_.scala.collection.mutable.ArrayBuffer(1,2,3,4)
println(a == b)
}
}
}
}
}
//串联式包语句
package com.hash.test{
object test5 {
def execute = {
//这里不会出错,因为如此定义com和com.hash下的都不可见
val a = collection.mutable.ArrayBuffer(1,2,3,4)
}
}
}
代码语言:javascript
复制package com.hash.test
package scala
object test6 {
def execute = {
println("文件顶部")
println("package com.hash.test")
println("package scala")
println("等同于")
println("package com.hash.test{")
println("package scala{")
println("直到文件末尾")
println("}")
println("}")
}
}
包对象:
代码语言:javascript
复制//由于JVM局限,包不能包含工具函数和常量,利用包对象
package com.hash.learn.scala
package object Chapter7 {
val defaultName = "HashZhang"
}
package Chapter7 {
object test7 {
def execute = {
println(defaultName)
}
}
}
包可见:
代码语言:javascript
复制package com.hash.learn.scala {
package Chapter7 {
object wc {
private[learn] def description = "I Can See U!";
}
}
}
package com.hash {
import com.hash.learn.scala.Chapter7.wc
object test8 {
def execute = {
//wc.description 在这里不可见
}
}
package learn {
import com.hash.learn.scala.Chapter7.wc
object test8 {
def execute = {
wc.description
}
}
}
}
重命名和隐藏:
代码语言:javascript
复制object renameAndHide {
//将Java中的HashMap重命名,同时导入所有包下类
import java.util.{HashMap => JavaHashMap, _}
import scala.collection.mutable.HashMap
def execute1 = {
val a = HashMap(1 -> 2)
val b = new JavaHashMap[Int, Int]()
}
import java.util.{HashMap => _}
import scala.collection.mutable.HashMap
def execute = {
val a = HashMap(1 -> 2)
}
//scala程序默认隐式引入:
//import java.lang._
//import scala._
//import Predef._
}