Teach Yourself Scheme in Fixnum Days [Go to first, previous, next page; contents; index]Chapter 12Objects and classesA classdescribes a collection of objectsthat share behavior. The objects described by a class are called the instancesof the class. The class specifies the names of the slotsthat the instance has, although it is up to the instance to populate these slots with particular values. The class also specifies the methodsthat 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 subclassof another class, which is called its superclass. A subclass not only has its own directslots 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 systemLet 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 #tas a ``zero'' superclass, one that has neither slots nor methods. The superclass of #tis 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 slotsand superclassrespectively. We will use twofields for methods, a method-namesfield that will hold the list of names of the class's methods, and a method-vectorfield that will hold the vector of the values of the class's methods.5Here is the definition of the standard-class:(defstruct standard-classslots 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]
has intentionally blurred sections.
Sign up to view the full version.