LISP - 對象系統(CLOS)

Common Lisp通過幾十年的面向對象編程的推進。但是,面向對象被併入是在它最後一階段。

類的定義

defclass宏允許創建用戶定義的類。它建立了一個類作爲數據類型。它的語法如下:

(DEFCLASS class-name (superclass-name*) (slot-description*) class-option*)

插槽是存儲數據變量或字段。

slot-description形式(插槽名稱插槽選項*),其中每個選項是一個關鍵字後跟一個名字,表達式和其他選項。最常用的槽選項是:

  • :accessor 函數名稱

  • :initform 表達式

  • :initarg 符號

例如,讓我們定義一個Box類,有三個槽的長度,廣度和高度。

(defclass Box () (length
breadth
height))

提供訪問和讀/寫控制到一個插槽

除非有插槽可以訪問,讀取或寫入的值,類是好看不中用。

當定義一個類可以爲每個插槽指定訪問。例如,把我們的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)))

類創建實例

通用函數make-instance創建並返回一個類的新實例。

它的語法如下:

(make-instance class {initarg value}*)

示例

讓我們創建一個Box類,有三個插槽,長度,寬度和高度。我們將使用三個插槽存取到這些字段設置的值。

創建一個名爲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))

當執行代碼,它返回以下結果:

Length of the Box is 10 Breadth of the Box is 10 Height of the Box is 5

定義一個類的方法

defmethod宏允許在類中定義一個方法。下面的示例擴展Box類包含一個方法名爲volume。

創建一個名爲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))

當執行代碼,它返回以下結果:

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允許在另一個對象來定義一個對象。這就是所謂的繼承。可以通過添加功能,新的或不同的創建派生類。派生類繼承了父類的功能。

下面的例子說明了這一點:

示例

創建一個名爲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 "Breadth of the Wooden Box is d%" (box-breadth item))
(format t "Height of the Wooden Box is d%" (box-height item))
(format t "Volume of the Wooden Box is d%" (volume item))
(format t "Price of the Wooden Box is d%" (box-price item))

當執行代碼,它返回以下結果:

Length of the Wooden Box is 10 Breadth of the Wooden Box is 10 Height of the Wooden Box is 5 Volume of the Wooden Box is 500 Price of the Wooden Box is 1000