File: CLOS-guide-3.html

package info (click to toggle)
cmucl 21d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,328 kB
  • sloc: lisp: 378,758; ansic: 30,673; asm: 2,977; sh: 1,417; makefile: 357; csh: 31
file content (119 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download | duplicates (12)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
<HTML>
<HEAD>
<TITLE>Brief Guide to CLOS: Instances</TITLE>
</HEAD>
<BODY>
<A HREF="CLOS-guide-2.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="CLOS-guide-4.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="CLOS-guide.html#toc3"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s3">3. Instances</A></H2>

<P>You can make an instance of a class with <CODE>MAKE-INSTANCE</CODE>.  It's
similar to the <CODE>MAKE-x</CODE> functions defined by <CODE>DEFSTRUCT</CODE> but lets you
pass the class to instantiate as an argument:</P>

<P>
<BLOCKQUOTE><CODE>
<PRE>
(MAKE-INSTANCE class {initarg value}*)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Instead of the class object itself, you can use its name.
For example:</P>

<P>
<BLOCKQUOTE><CODE>
<PRE>
(make-instance 'person :age 100)
</PRE>
</CODE></BLOCKQUOTE>
</P>

<P>This person object would have age <CODE>100</CODE> and name <CODE>BILL</CODE>, the default.</P>

<P>It's often a good idea to define your own constructor functions,
rather than call <CODE>MAKE-INSTANCE</CODE> directly, because you can hide
implementation details and don't have to use keyword parameters
for everything.  For instance, you might want to define</P>

<P>
<BLOCKQUOTE><CODE>
<PRE>
(defun make-person (name age)
  (make-instance 'person :name name :age age))
</PRE>
</CODE></BLOCKQUOTE>
</P>

<P>if you wanted the name and age to be required, positional parameters,
rather than keyword parameters.</P>

<P>The accessor functions can be used to get and set slot values:</P>

<P>
<BLOCKQUOTE><CODE>
<PRE>
&lt;cl&gt; (setq p1 (make-instance 'person :name 'jill :age 100))
#&lt;person @ #x7bf826&gt; 

&lt;cl&gt; (person-name p1)
jill 

&lt;cl&gt; (person-age p1)
100 

&lt;cl&gt; (setf (person-age p1) 101)
101 

&lt;cl&gt; (person-age p1)
101 
</PRE>
</CODE></BLOCKQUOTE>
</P>

<P>Note that when you use <CODE>DEFCLASS</CODE>, the instances are printed using
the <CODE>#&lt;...&gt;</CODE> notation, rather than 
as <CODE>#s(person :name jill :age 100)</CODE> .
But you can change the way instances are printed by defining methods
on the generic function <CODE>PRINT-OBJECT</CODE>.</P>
<P>Slots can also be accessed by name using <CODE>(SLOT-VALUE instance slot-name)</CODE>:</P>

<P>
<BLOCKQUOTE><CODE>
<PRE>
&lt;cl&gt; (slot-value p1 'name)
jill 

&lt;cl&gt; (setf (slot-value p1 'name) 'jillian)
jillian 

&lt;cl&gt; (person-name p1)
jillian 
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>You can find out various things about an instance by calling
<CODE>DESCRIBE</CODE>:</P>

<P>
<BLOCKQUOTE><CODE>
<PRE>
&lt;cl&gt; (describe p1)
#&lt;person @ #x7bf826&gt; is an instance 
 of class #&lt;clos:standard-class person @ #x7ad8ae&gt;:
The following slots have :INSTANCE allocation:
age     101
name    jillian
</PRE>
</CODE></BLOCKQUOTE>
</P>


<HR>
<A HREF="CLOS-guide-2.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="CLOS-guide-4.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="CLOS-guide.html#toc3"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>