notes49 - Within the default method the name message refers...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Within the default method, the name message refers to whatever message was sent. (The name args refers to a list containing any additional arguments that were used.) Let’s say we want to maintain a list of all the instances that have been created in a certain class. It’s easy enough to establish the list as a class variable, but we also have to make sure that each new instance automatically adds itself to the list. We do this with an initialize clause: ;;;;; In file cs61a/lectures/3.0/demo2.scm (define-class (counter) (instance-vars (count 0)) (class-vars (total 0) (counters ’())) (initialize (set! counters (cons self counters))) (method (next) (set! total (+ total 1)) (set! count (+ count 1)) (list count total))) > (define c1 (instantiate counter)) > (define c2 (instantiate counter)) > (ask counter ’counters) (#<procedure> #<procedure>) There was a bug in our pigger class definition; Scheme gets into an infinite loop if we ask Porky to greet , because it tries to translate the word...
View Full Document

This note was uploaded on 02/17/2010 for the course COMPUTER S 26275 taught by Professor Harvey,b during the Spring '10 term at Berkeley.

Ask a homework question - tutors are online