1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
<HTML>
<HEAD>
<TITLE>Brief Guide to CLOS: Inheritance of slot options</TITLE>
</HEAD>
<BODY>
<A HREF="CLOS-guide-3.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="CLOS-guide-5.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="CLOS-guide.html#toc4"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s4">4. Inheritance of slot options</A></H2>
<P>The class above had no superclass. That's why there was a
<CODE>"()"</CODE> after
<CODE>"defclass person"</CODE>. Actually, this means it
has one superclass: the
class <CODE>STANDARD-OBJECT</CODE>.</P>
<P>When there are superclasses, a subclass can specify a slot that has
already been specified for a superclass. When this happens, the
information in slot options has to be combined. For the slot options
listed above, either the option in the subclass overrides the one in
the superclass or there is a union:</P>
<P>
<DL>
<DT><B>:ACCESSOR</B><DD><P>union</P>
<DT><B>:INITARG</B><DD><P>union</P>
<DT><B>:INITFORM</B><DD><P>overrides</P>
</DL>
</P>
<P>This is what you should expect. The subclass can <B>change</B> the
default initial value by overriding the <CODE>:INITFORM</CODE>, and can add
to the initargs and accessors.</P>
<P>However, the "union" for accessor is just a consequence of how
generic functions work. If they can apply to instances of a
class C, they can also apply to instances of subclasses of C.</P>
<P>(Accessor functions are generic. This may become clearer
once generic functions are discussed, below.)</P>
<P>Here are some subclasses:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
<cl> (defclass teacher (person)
((subject :accessor teacher-subject
:initarg :subject)))
#<clos:standard-class teacher @ #x7cf796>
#<cl> (defclass maths-teacher (teacher)
((subject :initform "Mathematics")))
#<clos:standard-class maths-teacher @ #x7d94be>
<cl> (setq p2 (make-instance 'maths-teacher
:name 'john
:age 34))
#<maths-teacher @ #x7dcc66>
<cl> (describe p2)
#<maths-teacher @ #x7dcc66>; is an instance of
class #<clos:standard-class maths-teacher @ #x7d94be>:
The following slots have :INSTANCE allocation:
age 34
name john
subject "Mathematics"
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Note that classes print like
<CODE>#<clos:standard-class maths-teacher @ #x7d94be></CODE>.
The <CODE>#<...></CODE> notation usually has the form</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
#<class-of-the-object ... more information ...>
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>So an instance of maths-teacher prints as
<CODE>#<MATHS-TEACHER ...></CODE>.
The notation for the classes above indicates that they are
instances of <CODE>STANDARD-CLASS</CODE>. <CODE>DEFCLASS</CODE> defines standard classes.
<CODE>DEFSTRUCT</CODE> defines structure classes.</P>
<HR>
<A HREF="CLOS-guide-3.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="CLOS-guide-5.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="CLOS-guide.html#toc4"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>
|