建立沒有父項的基本 CLOS 類
CLOS 類描述如下:
- 一個名字
- 超類列表
- 插槽列表
- 更多選項,如文件
每個插槽都有:
- 一個名字
- 初始化表單(可選)
- 初始化引數(可選)
- 一種型別(可選)
- 文件字串(可選)
- 存取器,讀取器和/或寫入器功能(可選)
- 其他選項如分配
例:
(defclass person ()
((name
:initform "Erika Mustermann"
:initarg :name
:type string
:documentation "the name of a person"
:accessor person-name)
(age
:initform 25
:initarg :age
:type number
:documentation "the age of a person"
:accessor person-age))
(:documentation "a CLOS class for persons with name and age"))
預設列印方法:
(defmethod print-object ((p person) stream)
"The default print-object method for a person"
(print-unreadable-object (p stream :type t :identity t)
(with-slots (name age) p
(format stream "Name: ~a, age: ~a" name age))))
建立例項:
CL-USER > (make-instance 'person)
#<PERSON Name: Erika Mustermann, age: 25 4020169AB3>
CL-USER > (make-instance 'person :name "Max Mustermann" :age 24)
#<PERSON Name: Max Mustermann, age: 24 4020169FEB>