Skip to content
Extraits de code Groupes Projets
Valider 0b9ea737 rédigé par Nicolas Verbois's avatar Nicolas Verbois
Parcourir les fichiers

Update step_3b.rkt : adding comments and explanations, plus tweeking some details

parent 03964095
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#lang r5rs
; Authors : Nicolas Verbois - 1366 1600
; Nicolas Rosar - 1443 1900
; Group F
; In this step, we will introduce a form of inheritance, and to do so
; we have created two new classes : objects and color-points.
; Each class will now have in its state a reference, named super, which
; will be pointing to its (unique) parent class.
; And now, when a message is called on a receiver object and the class of that object
; does not define that method, the message lookup will look in the superclass
; instead (and so on).
; The new object class at the root of our inheritance hierarchy.
; An object has no inner parameters, and is only defined by its type.
; This is where the lookup loop for messages will stop, and thus
; it is in this class that we will eventually display if a message
; is not understood.
(define (object)
(define (gettype) 'object)
(define (self m)
......@@ -5,23 +26,47 @@
(else (display "Message not understood"))))
self)
; A color-point is a child class of a point, meaning that like a point,
; a color-point has x and y coordinates, but is also defined by a color
; parameter.
; Thus, we have implemented inheritance by defining, inside the definiton of
; a color-point, a parent point whose name is "super".
; We have then redefined methods of the class color-point where needed, and
; more importantly delegated to the super class when we were able to.
; For example, to create a list containing the informations about the color-point,
; we will call the super class to get the x and y coordinates, since the methods to
; retrieve those are already implemented inside the point class.
; Also, we have modified the dispatcher (self method) to send messages to the
; super class in case the color-point class is not capable of handling it.
; This constitues the lookup loop mechanism of inheritance.
(define (color-point x y color)
(define mypoint (point x y))
(define super (point x y))
(define (getcolor) color)
(define (gettype) 'color-point)
(define (getinfo) (list (self 'type) (mypoint 'getx) (mypoint 'gety) (self 'get-color)))
(define (getinfo) (list (self 'type) (super 'getx) (super 'gety) (self 'get-color)))
(define (add cp)
(color-point (+ (mypoint 'getx) (cp 'getx)) (+ (mypoint 'gety) (cp 'gety)) (getcolor)))
(color-point (+ (super 'getx) (cp 'getx)) (+ (super 'gety) (cp 'gety)) (getcolor)))
(define (self m)
(cond ((eq? m 'get-color) (getcolor))
((eq? m 'info) (getinfo))
((eq? m 'type) (gettype))
((eq? m 'add) add)
(else (send mypoint m))))
(else (send super m))))
self)
; Finally, the point class has had only a few changes.
; First, we have added a reference to the parent class of a point,
; being an object named super.
; Then, we have only modified the dispatcher (self method) to send
; a message to the super class in case the point class cannot handle it.
(define (point x y)
(define myobject (object))
(define super (object))
(define (getx) x )
(define (gety) y )
(define (gettype) 'point )
......@@ -38,9 +83,11 @@
((eq? m 'setx!) setx)
((eq? m 'sety!) sety)
((eq? m 'add) add)
(else (send myobject m))))
(else (send super m))))
self)
(define (send p m . args)
(if (procedure? p)
(cond
......@@ -51,32 +98,63 @@
)
; Test part 2
( define o ( object ) ) (newline)
( send o ' type ) ; o b j e c t
( send o ' foo ) ; should d i s p l a y ” Message not understood ”
; Sample code illustrating the execution of the step
; We start by creating a simple object, and checking its type as well as the lookup stop.
(define o (object))
(display (send o 'type)) ; should display : object
(newline)
(display (send o 'foo)) ; should display : ” Message not understood ”
(newline)
( define p1 ( point 1 2 ) )
( define p2 ( point 3 4 ) )
( send p1 ' getx ) ; 1
( send p1 ' gety ) ; 2
( send p2 ' getx ) ; 3
( send p2 ' gety ) ; 4
(display '----------)
(newline)
( define p ( send p1 'add p2 ) )
( send p 'info ) ; ( point 4 6)
; We then test thee creation of two standard points, and verify that
; the different methods from previous steps still work as intended.
(define p1 (point 1 2))
(define p2 (point 3 4))
(display (send p1 'getx)) ; should display : 1
(newline)
(display (send p1 'gety)) ; should display : 2
(newline)
(display (send p2 'getx)) ; should display : 3
(newline)
(display (send p2 'gety)) ; should display : 4
(newline)
(define p (send p1 'add p2))
(display (send p 'info)) ; should display : (point 4 6)
(newline)
(display '----------)
(newline)
(newline)
( define cp ( color-point 5 6 'red ) )
( send cp ' type ) ; color-point
( send cp ' getx ) ; 5
( send cp ' gety ) ; 6
( send cp ' get-color ) ; red
( send cp ' info )
( define cp-1 ( send cp 'add ( color-point 1 2 'green ) ) )
( send cp-1 'type ) ; color-point
( send cp-1 'getx ) ; 6
( send cp-1 'gety ) ; 8
( send cp-1 'get-color ) ; red
\ No newline at end of file
(display (send cp 'type)) ; should display : color-point
(newline)
(display (send cp 'getx)) ; should display : 5
(newline)
(display (send cp 'gety)) ; should display : 6
(newline)
(display (send cp 'get-color)) ; should display : red
(newline)
(display (send cp 'info)) ; should display : (color-point 5 6 red)
(newline)
(display '----------)
(newline)
; Adding up two color-point objects should yield a new color-point with coordinates being summed
; and with as color the color of the receiver.
(define cp-1 (send cp 'add (color-point 1 2 'green)))
(display (send cp-1 'type)) ; should display : color-point
(newline)
(display (send cp-1 'getx)) ; should display : 6
(newline)
(display (send cp-1 'gety)) ; should display : 8
(newline)
(display (send cp-1 'get-color)) ; should display : red (which is the color of the receiver color-point)
(newline)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter