程序设计科学/Sway演示文稿/对象/闭包作为对象
外观
< 程序设计科学 | Sway演示文稿/对象
闭包作为对象
来自《计算机程序的结构与解释》
(define (account amount) (define (deposit d) (set! amount (+ amount d))) (define (withdraw w) (set! amount (- amount w))) (lambda (msg arg) (cond ((eq msg 'withdraw) (withdraw arg)) ((eq msg 'deposit) (deposit arg)) (else (error "unknown message: " msg)) ) ) )
现在我们可以将“account”当作构造函数处理
(define a (account 100)) (a'withdraw 10) (a'deposit 20)
将消息转换为函数调用的闭包是一种技巧。