Chapter 12

Chapter 12 - Teach Yourself Scheme in Fixnum Days [Go to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Teach Yourself Scheme in Fixnum Days [Go to first , previous , next page; contents ; index ] Chapter 12 Objects and classes A class describes a collection of objects that share behavior. The objects described by a class are called the instances of the class. The class specifies the names of the slots that the instance has, although it is up to the instance to populate these slots with particular values. The class also specifies the methods that can be applied to its instances. Slot values can be anything, but method values must be procedures. Classes are hierarchical. Thus, a class can be a subclass of another class, which is called its superclass . A subclass not only has its own direct slots and methods, but also inherits all the slots and methods of its superclass. If a class has a slot or method that has the same name as its superclass's, then the subclass's slot or method is the one that is retained. 12.1 A simple object system Let us now implement a basic object system in Scheme. We will allow only one superclass per class ( single inheritance ). If we don't want to specify a superclass, we will use #t as a ``zero'' superclass, one that has neither slots nor methods. The superclass of #t is deemed to be itself. As a first approximation, it is useful to define classes using a struct called standard-class , with fields for the slot names, the superclass, and the methods. The first two fields we will call slots and superclass respectively. We will use two fields for methods, a method-names field that will hold the list of names of the class's methods, and a method-vector field that will hold the vector of the values of the class's methods. 5 Here is the definition of the standard-class : (defstruct standard-class slots superclass method-names method-vector) We can use make-standard-class , the maker procedure of standard-class , to create a new class. Eg, (define trivial-bike-class (make-standard-class 'superclass #t 'slots '(frame parts size) 'method-names '() file:///C|/Documents%20and%20Settings/Linda%20Grauer. ..tes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-14.html (1 of 9) [2/6/2008 11:41:16 AM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Teach Yourself Scheme in Fixnum Days 'method-vector #())) This is a very simple class. More complex classes will have non-trivial superclasses and methods, which will require a lot of standard initialization that we would like to hide within the class creation process. We will therefore define a macro called create-class that will make the appropriate call to make-standard- class . (define-macro create-class (lambda (superclass slots . methods) `(create-class-proc ,superclass (list ,@(map (lambda (slot) `',slot) slots)) (list ,@(map (lambda (method) `',(car method)) methods)) (vector ,@(map (lambda (method) `,(cadr method)) methods))))) We will defer the definition of the create-class-proc procedure to later. The procedure
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 4536 taught by Professor Fisler during the Winter '08 term at WPI.

Page1 / 9

Chapter 12 - Teach Yourself Scheme in Fixnum Days [Go to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online