Книга: Practical Common Lisp
DEFCLASS
DEFCLASS
You create user-defined classes with the DEFCLASS
macro. Because behaviors are associated with a class by defining generic functions and methods specialized on the class, DEFCLASS
is responsible only for defining the class as a data type.
The three facets of the class as a data type are its name, its relation to other classes, and the names of the slots that make up instances of the class.[182] The basic form of a DEFCLASS
is quite simple.
(defclass name (direct-superclass-name*)
(slot-specifier*))
What Are "User-Defined Classes"? |
The term user-defined classes isn't a term from the language standard—technically what I'm talking about when I say user-defined classes are classes that subclass |
As with functions and variables, you can use any symbol as the name of a new class.[183] Class names are in a separate namespace from both functions and variables, so you can have a class, function, and variable all with the same name. You'll use the class name as the argument to MAKE-INSTANCE
, the function that creates new instances of user-defined classes.
The direct-superclass-names specify the classes of which the new class is a subclass. If no superclasses are listed, the new class will directly subclass STANDARD-OBJECT
. Any classes listed must be other user-defined classes, which ensures that each new class is ultimately descended from STANDARD-OBJECT
. STANDARD-OBJECT
in turn subclasses T
, so all user-defined classes are part of the single class hierarchy that also contains all the built-in classes.
Eliding the slot specifiers for a moment, the DEFCLASS
forms of some of the classes you used in the previous chapter might look like this:
(defclass bank-account () ...)
(defclass checking-account (bank-account) ...)
(defclass savings-account (bank-account) ...)
I'll discuss in the section "Multiple Inheritance" what it means to list more than one direct superclass in direct-superclass-names.