University of Virginia Computer Science
CS150: Computer Science, Fall 2005
14 October 2005
Inheritance is using the definition of one class to make another class.(define (make-number n) (lambda (message) (cond ((eq? message 'value) (lambda (self) n)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value)))))))) (define (ask object message . args) (apply (object message) object args))
The class fraction inherits from number.(define (make-fraction numer denom) (let ((super (make-number #f))) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else (super message))))))
Which of the traced applications are from fraction and which are from number?
> (ask half 'add half) |(ask #
add # ) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask # value) | |(eq? value value) | |#t | 1/2 | (ask # value) | |(eq? value value) | |#t | 1/2 |1 1
If I have seen further it is by standing
on the shoulders of giants.
Isaac Newton (on inheritance?)
If I have not seen as far as others, it is because
giants were standing on my shoulders.
Hal Abelson (on subtyping?)
|"); print ( $res[$first] ) ; print ("|
CS 150: Computer Science
University of Virginia
Using these Materials