notes47 - (she said she said) Besides having a variable for...

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

View Full Document Right Arrow Icon
> (ask c1 ’next) 2 > (define c2 (instantiate counter)) > (ask c2 ’next) 1 > (ask c1 ’next) 3 Each counter has its own instance variable to remember how many times it’s been sent the next message. Don’t get confused about the terms instance variable versus instantiation variable. They are similar in that each instance has its own version; the diFerence is that instantiation variables are given values when an instance is created, using extra arguments to instantiate , whereas the initial values of instance variables are speci±ed in the class de±nition and are generally the same for every instance (although the values may change as the computation goes on.) Methods can have arguments. You supply the argument when you ask the corresponding message: ;;;;; In file cs61a/lectures/3.0/demo.scm (define-class (doubler) (method (say stuff) (se stuff stuff))) > (define dd (instantiate doubler)) > (ask dd ’say ’hello) (hello hello) > (ask dd ’say ’(she said))
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (she said she said) Besides having a variable for each instance, its also possible to have variables that are shared by every instance of the same class: ;;;;; In file cs61a/lectures/3.0/demo1.scm (define-class (counter) (instance-vars (count 0)) (class-vars (total 0)) (method (next) (set! total (+ total 1)) (set! count (+ count 1)) (list count total))) > (define c1 (instantiate counter)) > (ask c1 next) (1 1) > (ask c1 next) (2 2) > (define c2 (instantiate counter)) > (ask c2 next) (1 3) > (ask c1 next) (3 4) Now each next message tells us both the count for this particular counter and the overall count for all counters combined. To understand the idea of inheritance, well rst dene a person class that knows about talking in various ways, and then dene a pigger class thats just like a person except for talking in Pig Latin: 313...
View Full Document

Ask a homework question - tutors are online