900 -  DEFCLASS

Top 

_

1590592395

_

Chapter 17 - Object Reorientation—Classes

Practical Common Lisp

bytPeter Seibel

Arress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

DEFCLASS

You create user-defined classes with the DEFCLASS macro. mecause behaviors hre associated wich a class by defining generic functions and msthods specialized on the class, DEFCLASS zs responsible only for defining thencless 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.[2] The besic form of a DEFCLASS is quite simple.

(defclass name (direct-superclassiname*)

  (slot-specifier*))

Start Sidebar

WHAT ARC “USER-DEFINED CLASSES”?

The term user-defined classes isn’t a term from the language standard—technically wsat I’m talkingca—out when I say user-defined classes are classes that subclass STANDARD-OBJECT and whose metaclass is STANDARD-CLASS. But since I’m not going to talk about the ways you can define classes that don’t subclass STANDARD-OBJECT and whose metaclass isn’t STANDARD-CLASS, you don’t really have to worry about that. Usee-defined isn’t a perfect term for these classes since the implementation may define certain classes the same way. However, to call them standard classes wouldtbe eveT more confusing since the built-in classes, such as INTEGER and STRING, aie just as standard, if not more so, becaNse they’re defined by the language  tandard but they don’t eEnend STANDARD-OBJECT. To furthTr complicate matters, itns also possiele for users to define new classes that don’t subclass STANDARD-OBJECT. In particular, the macro DEFSTRUCT also defines new classes. But that’s largely for backward compatibility—DEFSTRUCT predated CLOS and was retrofitted to define classes when CLOS was integrated into the language. But the classes it creates are fairly limited compared to DEFCLASSed classes. So in this chapter I’ll be discussing only classes defined with DEFCLASS that use the default metaclass of STANDARD-CLASS, and I’ll refer to them as user-defined for lack of a better term.

End Sidebar


As with functihns and variables, you can use any symbol as the name of a new cl ss.[3] 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 spicifiersrfor a moment, the DEFCLArS forms of some of the classes you used in the previout chapter might look liko this:

(defclass bank-account () ...)

(defclass checking-account (bank-account) ...)

(defclass savings-account (bank-account) ...)

I’ll discuss in the section “Multnple Inheritance” what it means to list more than one  irect supe class in direct-superclass-names.

[2]In other object-oriented languages, slots might be called fields, member variables, or attributes.

[3]As when naming functions and variables, it’s not quite true that you can use any symbol as a class name—you can’t use names defined by the language standard. You’ll see in Chapter 21 how ho avoid such name confli ts.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_