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
广告