可重新赋值的变量和属性
对可重新赋值的变量执行两种操作:获取它的值或者设定为新的值。
//定义公开变量,公开变量扩展为getter和setter方法
object Ex4 {
class Time {
var hour = 12
var minute = 0
}
def main(args: Array[String]) {
val time = new Time
println("time.hour [" + (time.hour) + "]")
println("time.minute [" + (time.minute) + "]")
time.hour = 13
time.minute = 14
println("time.hour [" + (time.hour) + "]")
println("time.minute [" + (time.minute) + "]")
}
}
//直接定义getter和setter方法
object Ex6 {
class Time {
private[this] var h = 12
private[this] var m = 12
def hour: Int = h
def hour_= (x: Int) {
require(0 <= x && x < 24)
h = x
}
def minute = m
def minute_= (x: Int) {
require(0 <= x && x < 60)
m = x
}
}
def main(args: Array[String]) {
val time = new Time
println("time.hour [" + (time.hour) + "]")
println("time.minute [" + (time.minute) + "]")
time.hour = 13
time.minute = 14
println("time.hour [" + (time.hour) + "]")
println("time.minute [" + (time.minute) + "]")
try {
time.minute = 75
} catch {
case ex: IllegalArgumentException => println("caught exception")
}
}
}
//定义不带关联字段的getter和setter
object Ex7 {
class Thermometer {
var celsius: Float = _ //初始化变量设定为缺省值
def fahrenheit = celsius * 9 / 5 + 32
def fahrenheit_= (f: Float) {
celsius = (f - 32) * 5 / 9
}
override def toString = fahrenheit +"F/"+ celsius +"C"
}
def main(args: Array[String]) {
val t = new Thermometer
t.celsius = 100
println("t [" + (t) + "]")
t.fahrenheit = -40
println("t [" + (t) + "]")
}
}
val t=new Ex7.Thermometer
//t: Ex7.Thermometer = 32.0F/0.0C
t.celsius
//res20: Float = 0.0
t.celsius=100
//t.celsius: Float = 100.0
println("t [" + (t) + "]")
//t [212.0F/100.0C]
离散事件模拟
//数字电路模拟
object Simulator2 {
abstract class BasicCircuitSimulation extends Simulation {
def InverterDelay: Int //反转器,输出反转信号
def AndGateDelay: Int //与门,输出其输入信号的合取值
def OrGateDelay: Int //或门,输出其输入信号的析取值
//wire 线路
class Wire {
private var sigVal = false
private var actions: List[Action] = List()
def getSignal = sigVal
def setSignal(s: Boolean) =
if (s != sigVal) {
sigVal = s
actions foreach (_ ())
}
def addAction(a: Action) = {
actions = a :: actions
a()
}
}
def inverter(input: Wire, output: Wire) = {
def invertAction() {
val inputSig = input.getSignal
afterDelay(InverterDelay) {
output setSignal !inputSig
}
}
input addAction invertAction
}
def andGate(a1: Wire, a2: Wire, output: Wire) = {
def andAction() = {
val a1Sig = a1.getSignal
val a2Sig = a2.getSignal
afterDelay(AndGateDelay) {
output setSignal (a1Sig & a2Sig)
}
}
a1 addAction andAction
a2 addAction andAction
}
def orGate(o1: Wire, o2: Wire, output: Wire) {
def orAction() {
val o1Sig = o1.getSignal
val o2Sig = o2.getSignal
afterDelay(OrGateDelay) {
output setSignal (o1Sig | o2Sig)
}
}
o1 addAction orAction
o2 addAction orAction
}
def probe(name: String, wire: Wire) {
def probeAction() {
println(name +" "+ currentTime +
" new-value = "+ wire.getSignal)
}
wire addAction probeAction
}
}
//SimulationAPI
//私有变量和公开访问方法的组合可以用来保证当前时间不背Simulation类外部的方法改变。
abstract class Simulation {
type Action = () => Unit
case class WorkItem(time: Int, action: Action)
private var curtime: Int = 0
def currentTime: Int = curtime
private var agenda: List[WorkItem] = List()
private def insert(ag: List[WorkItem],
item: WorkItem): List[WorkItem] = {
if (ag.isEmpty || item.time < ag.head.time) item :: ag
else ag.head :: insert(ag.tail, item)
}
def afterDelay(delay: Int)(block: => Unit) {
val item = WorkItem(currentTime + delay, () => block)
agenda = insert(agenda, item)
}
private def next() {
(agenda: @unchecked) match {
case item :: rest =>
agenda = rest
curtime = item.time
item.action()
}
}
def run() {
afterDelay(0) {
println("*** simulation started, time = "+
currentTime +" ***")
}
while (!agenda.isEmpty) next()
}
}
abstract class CircuitSimulation
extends BasicCircuitSimulation {
//半加法电路
def halfAdder(a: Wire, b: Wire, s: Wire, c: Wire) {
val d, e = new Wire
orGate(a, b, d)
andGate(a, b, c)
inverter(c, e)
andGate(d, e, s)
}
//全加法器
def fullAdder(a: Wire, b: Wire, cin: Wire,
sum: Wire, cout: Wire) {
val s, c1, c2 = new Wire
halfAdder(a, cin, s, c1)
halfAdder(b, s, sum, c2)
orGate(c1, c2, cout)
}
}
}
object MySimulation2 extends Simulator2.CircuitSimulation {
def InverterDelay = 1
def AndGateDelay = 3
def OrGateDelay = 5
def main(args: Array[String]) {
val input1, input2, sum, carry = new Wire
probe("sum", sum)
probe("carry", carry)
halfAdder(input1, input2, sum, carry)
input1 setSignal true
run()
input2 setSignal true
run()
}
}