我有以下類,而更像是他們:如何快速爲班級創建許多類似的老虎機?
(defclass weapon()
((base-slice-damage
:documentation "Base slice damage dealt by weapon"
:reader base-slice-damage
:initform 0
:initarg :base-slice-damage)
(base-blunt-damage
:reader base-blunt-damage
:initform 0
:initarg :base-blunt-damage)
(base-pierce-damage
:reader base-pierce-damage
:initform 0
:initarg :base-pierce-damage)))
(defclass dagger (weapon)
((base-slice-damage
:initform 3)
(base-pierce-damage
:initform 6)))
(defclass attack()
((slice-damage-dealt
:initarg :slice-damage-dealt
:reader slice-damage-dealt)
(blunt-damage-dealt
:initarg :blunt-damage-dealt
:reader blunt-damage-dealt)
(pierce-damage-dealth
:initarg :pierce-damage-dealt
:reader pierce-damage-dealt)))
正如你可以看到,有很多重複的。對於其中兩個類,我的插槽都具有相同的選項,並且僅根據它們是切片,鈍器還是穿孔而變化。 。
我想過使用宏來定義屬性類,然後就混合那些這是我到目前爲止有:
(defmacro defattrclass (attr-name &body class-options)
`(defclass ,(symb attr-name '-attr)()
((,attr-name
,@class-options))))
但這確實還遠遠不夠。
編輯:
我想出這個,雖然我不完全滿意的:
(defmacro defattrclass (attr-name &body class-options)
`(defclass ,(symb attr-name '-attr)()
((,attr-name
,@class-options))))
(defmacro defattrclasses (attr-names &body class-options)
`(progn
,@(loop for attr-name in attr-names collect
`(defattrclass ,attr-name ,@class-options))))
http://www.reddit.com/r/lisp/comments/qwy5o/how_can_i_quickly_create_many_similar_slots_for_a/(這是很好的網絡禮儀是明確的,如果你要問的問題在兩個地方) – 2012-03-15 00:11:18
謝謝,我沒有意識到 – higginbotham 2012-03-15 19:44:11