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 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
<HTML>
<HEAD>
<TITLE>Brief Guide to CLOS: Defining classes.</TITLE>
</HEAD>
<BODY>
<A HREF="CLOS-guide-1.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="CLOS-guide-3.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="CLOS-guide.html#toc2"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s2">2. Defining classes.</A></H2>
<P>You define a class with <CODE>DEFCLASS</CODE> :</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
(DEFCLASS class-name (superclass-name*)
(slot-description*)
class-option*)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>For simple things, forget about class options.</P>
<P>A slot-description has the form <CODE>(slot-name slot-option*)</CODE> , where each
option is a keyword followed by a name, expression, or whatever. The
most useful slot options are</P>
<P>
<DL>
<DT><B>:ACCESSOR</B><DD><P>function-name</P>
<DT><B>:INITFORM</B><DD><P>expression</P>
<DT><B>:INITARG</B><DD><P>symbol</P>
</DL>
</P>
<P>(Initargs are usually keywords.)</P>
<P><CODE>DEFCLASS</CODE> is similar to <CODE>DEFSTRUCT</CODE>. The syntax is a bit different, and
you have more control over what things are called. For instance,
consider the <CODE>DEFSTRUCT</CODE>:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
(defstruct person
(name 'bill)
(age 10))
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P><CODE>DEFSTRUCT</CODE> would automatically define slots with expressions to
compute default initial values, access-functions like <CODE>PERSON-NAME</CODE>
to get and set slot values, and a <CODE>MAKE-PERSON</CODE> that took keyword
initialization arguments <CODE>(initargs)</CODE> as in</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
(make-person :name 'george :age 12)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>A <CODE>DEFCLASS</CODE> that provided similar access functions, etc, would be:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
(defclass person ()
((name :accessor person-name
:initform 'bill
:initarg :name)
(age :accessor person-age
:initform 10
:initarg :age)))
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Note that <CODE>DEFCLASS</CODE> lets you control what things are called. For
instance, you don't have to call the accessor <CODE>PERSON-NAME</CODE>. You could
call it <CODE>NAME</CODE>.</P>
<P>In general, you should pick names that make sense for a group of
related classes rather than rigidly following the <CODE>DEFSTRUCT</CODE>
conventions.</P>
<P>You do not have to provide all options for every slot.
Maybe you don't want it to be possible to initialize a slot
when calling <CODE>MAKE-INSTANCE</CODE> (for which see below). In that case,
don't provide an <CODE>:INITARG</CODE>. Or maybe there isn't a meaningful
default value. (Perhaps the meaningful values will always be
specified by a subclass.) In that case, no <CODE>:INITFORM</CODE>.</P>
<P>Note that classes are objects. To get the class object from its
name, use <CODE>(FIND-CLASS name)</CODE>. Ordinarily, you won't need to do this.</P>
<HR>
<A HREF="CLOS-guide-1.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="CLOS-guide-3.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="CLOS-guide.html#toc2"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>
|