(defclass Class-Name (Superclass*) (Slot-Definition*) Class-Option*)[1]
(defclass Graphical-Object () ())
(defclass Rectangle (Graphical-Object) ((Height :accessor Height :initarg :Height :initform 3) (Width :accessor Width :initarg :Width :initform 5)))
(defclass Square (Rectangle) () (:documentation "The SQUARE class"))
(defclass Submarine () ((Depth) (Speed))) (setq Sub-1 (make-instance 'Submarine)) (setf (slot-value Sub-1 'Depth) 100) (slot-value Sub-1 'Depth) ==> 100 (slot-value Sub-1 'Speed) ==> [Error: unbound slot SPEED]Unlike with the methods defined by :reader, :writer, and :accessor, you cannot put :before or :after methods on slot-value or (setf slot-value).
There are also functions slot-exists-p and slot-boundp for checking if a given instance has a certain slot and if that slot has a value, respectively.
:initform a default value for the slot (unbound otherwise)
:reader the name of a function to lookup the slot
:writer the name of a function to set the slot
:accessor the name of a function to read, and (with setf) set the slot
:initarg the name of a keyword for make-instance
:allocation whether slot is shared or specific to each instance
:type a Common Lisp type for use in compiler optimizations
:documentation documentation for the very rarely-seen slot object
Values supplied by :initform, :allocation, and :documentation override any previously supplied value in a superclass. Values supplied via :reader, :writer, :accessor and :initarg give values that apply to the current class in addition to values supplied for superclasses. A :type restriction imposed on a slot is intersected with the restrictions on that slot imposed by superclasses. Similarly, you can only supply one :initform, :allocation, :documentation, or :type entry for a given slot, but you can supply multiple :reader, :writer, :accessor or :initarg entries.
(defclass Submarine () ((Depth :initform 100) (Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (slot-value 'Sub-1 'Speed) ==> 5 (slot-value 'Sub-1 'Depth) ==> 100
(defclass Submarine () ((Depth :reader Depth :initform 100) (Speed :reader Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5
(defclass Submarine () ((Depth :reader Depth :writer Set-Depth :initform 100) (Speed :reader Speed :writer Set-Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5 (Set-Depth 200 Sub-1) (Set-Speed 4 Sub-1) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 4
(defclass Submarine () ((Depth :accessor Depth :initform 100) (Speed :accessor Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine)) (Depth Sub-1) ==> 100 (Speed Sub-1) ==> 5 (setf (Depth Sub-1) 200) (setf (Speed Sub-1) 4) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 4
(defclass Submarine () ((Depth :accessor Depth :initarg :Depth :initform 100) (Speed :accessor Speed :initarg :Speed :initform 5))) (setq Sub-1 (make-instance 'Submarine :Depth 200)) (Depth Sub-1) ==> 200 (Speed Sub-1) ==> 5
(defclass Submarine () ((Depth :accessor Depth :initform 100) (Speed :accessor Speed :initform 5))) (defclass Los-Angeles (Submarine) ((Max-Speed :accessor Max-Speed :initform 10 :allocation :class))) (setq SSN-1 (make-instance 'Los-Angeles)) (setq SSN-2 (make-instance 'Los-Angeles)) (Max-Speed SSN-1) ==> 10 (Max-Speed SSN-2) ==> 10 (setf (Max-Speed SSN-1) 15) (Max-Speed SSN-1) ==> 15 (Max-Speed SSN-2) ==> 15
The :type entry gets inherited by combining the type restrictions of the current class and all superclasses using and. This means that a class cannot relax a type restriction imposed by a superclass, only make it more stringent.
(defclass Submarine () ((Depth :type integer))) (defclass SSN (Submarine) ((Depth :type fixnum)))This means that a legal value of the Depth slot must satisfy (typep value '(and integer fixnum)).
:documentation documentation for the class object
:default-initargs default values for the previously specified :initarg's
:metaclass the superclass of the class object itself. Leave this for the CLOS wizards.
Each class option can be specified at most once for a given class.
(setq Submarine-Class
(defclass Submarine ()
((Depth :accessor Depth :initform 100)
(Speed :accessor Speed :initform 5))
(:documentation "The basic submarine class")) )
(setq Sub-1 (make-instance 'Submarine))
(documentation Submarine-Class) ==> "The basic submarine class"
(documentation (class-of Sub-1)) ==> "The basic submarine class"
(documentation (find-class 'Submarine)) ==> "The basic submarine class"
(defclass Submarine ()
((Depth :accessor Depth :initarg :Depth :initform 100)
(Speed :accessor Speed :initarg :Speed :initform 5)))
(defclass Slow-Submarine (Submarine)
()
(:default-initargs
:Speed 1))
(setq Sub-1 (make-instance 'Slow-Submarine))
(Speed Sub-1) ==> 1