Lisp - 類



Common LISP 比面向物件程式設計的興起早了二十年左右。但是,面向物件後來被整合到了其中。

定義類

defclass 宏允許建立使用者自定義類。它將類建立為一種資料型別。它具有以下語法:

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

槽是儲存資料或欄位的變數。

槽描述具有以下形式:(槽名稱 槽選項*),其中每個選項都是一個關鍵字,後跟名稱、表示式和其他選項。最常用的槽選項是:

  • :accessor 函式名稱

  • :initform 表示式

  • :initarg 符號

例如,讓我們定義一個 Box 類,它具有三個槽:length、breadth 和 height。

(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 類,它具有三個槽:length、breadth 和 height。我們將使用三個槽訪問器來設定這些欄位中的值。

建立一個名為 main.lisp 的新原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; define a class box with accessor methods for length, breadth and height
(defclass box ()
   ((length :accessor box-length)
      (breadth :accessor box-breadth)
      (height :accessor box-height)
   )
)
; create an instance of class box and assign to an item
(setf item (make-instance 'box))
; set length of an item as 10
(setf (box-length item) 10)
; set breadth of an item as 10
(setf (box-breadth item) 10)
; set height of an item as 5 
(setf (box-height item) 5)
; print length of the box
(format t "Length of the Box is ~d~%" (box-length item))
; print breadth of the box
(format t "Breadth of the Box is ~d~%" (box-breadth item))
; print height of the box
(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 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; define a class box with accessor methods for length, breadth and height
; add a method reader
(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 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; define a class box with accessor methods for length, breadth and height
(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
廣告

© . All rights reserved.