Common Lisp通過幾十年的面向?qū)ο缶幊痰耐七M(jìn)。但是,面向?qū)ο蟊徊⑷胧窃谒詈笠浑A段。
defclass宏允許創(chuàng)建用戶定義的類。它建立了一個類作為數(shù)據(jù)類型。它的語法如下:
(DEFCLASS class-name (superclass-name*) (slot-description*) class-option*)
插槽是存儲數(shù)據(jù)變量或字段。
slot-description形式(插槽名稱插槽選項*),其中每個選項是一個關(guān)鍵字后跟一個名字,表達(dá)式和其他選項。最常用的槽選項是:
:accessor 函數(shù)名稱
:initform 表達(dá)式
:initarg 符號
例如,讓我們定義一個Box類,有三個槽的長度,廣度和高度。
(defclass Box () (length breadth height))
除非有插槽可以訪問,讀取或?qū)懭氲闹?,類是好看不中用?/p>
當(dāng)定義一個類可以為每個插槽指定訪問。例如,把我們的Box類:
(defclass Box () ((length :accessor length) (breadth :accessor breadth) (height :accessor height)))
也可以讀取和寫入一個插槽指定單獨的訪問器的名稱。
(defclass Box () ((length :reader get-length :writer set-length) (breadth :reader get-breadth :writer set-breadth) (height :reader get-height :writer set-height)))
通用函數(shù)make-instance創(chuàng)建并返回一個類的新實例。
它的語法如下:
(make-instance class {initarg value}*)
示例
讓我們創(chuàng)建一個Box類,有三個插槽,長度,寬度和高度。我們將使用三個插槽存取到這些字段設(shè)置的值。
創(chuàng)建一個名為main.lisp一個新的源代碼文件,并在其中輸入如下代碼:
(defclass box () ((length :accessor box-length) (breadth :accessor box-breadth) (height :accessor box-height))) (setf item (make-instance 'box)) (setf (box-length item) 10) (setf (box-breadth item) 10) (setf (box-height item) 5) (format t "Length of the Box is ~d~%" (box-length item)) (format t "Breadth of the Box is ~d~%" (box-breadth item)) (format t "Height of the Box is ~d~%" (box-height item))
當(dāng)執(zhí)行代碼,它返回以下結(jié)果:
Length of the Box is 10 Breadth of the Box is 10 Height of the Box is 5
defmethod宏允許在類中定義一個方法。下面的示例擴展Box類包含一個方法名為volume。
創(chuàng)建一個名為main.lisp一個新的源代碼文件,并在其中輸入如下代碼:
(defclass box () ((length :accessor box-length) (breadth :accessor box-breadth) (height :accessor box-height) (volume :reader volume))) ; method calculating volume (defmethod volume ((object box)) (* (box-length object) (box-breadth object)(box-height object))) ;setting the values (setf item (make-instance 'box)) (setf (box-length item) 10) (setf (box-breadth item) 10) (setf (box-height item) 5) ; displaying values (format t "Length of the Box is ~d~%" (box-length item)) (format t "Breadth of the Box is ~d~%" (box-breadth item)) (format t "Height of the Box is ~d~%" (box-height item)) (format t "Volume of the Box is ~d~%" (volume item))
當(dāng)執(zhí)行代碼,它返回以下結(jié)果:
Length of the Box is 10 Breadth of the Box is 10 Height of the Box is 5 Volume of the Box is 500
LISP允許在另一個對象來定義一個對象。這就是所謂的繼承。可以通過添加功能,新的或不同的創(chuàng)建派生類。派生類繼承了父類的功能。
下面的例子說明了這一點:
示例
創(chuàng)建一個名為main.lisp一個新的源代碼文件,并在其中輸入如下代碼:
(defclass box () ((length :accessor box-length) (breadth :accessor box-breadth) (height :accessor box-height) (volume :reader volume))) ; method calculating volume (defmethod volume ((object box)) (* (box-length object) (box-breadth object)(box-height object))) ;wooden-box class inherits the box class (defclass wooden-box (box) ((price :accessor box-price))) ;setting the values (setf item (make-instance 'wooden-box)) (setf (box-length item) 10) (setf (box-breadth item) 10) (setf (box-height item) 5) (setf (box-price item) 1000) ; displaying values (format t "Length of the Wooden Box is ~d~%" (box-length item)) (format t "Br