Unformatted text preview: (she said she said) Besides having a variable for each instance, it’s 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, we’ll ±rst de±ne a person class that knows about talking in various ways, and then de±ne a pigger class that’s just like a person except for talking in Pig Latin: 313...
View Full Document
- Spring '10
- Object-Oriented Programming, Subroutine, instance variable versus