目录

LISP - CLOS

共同的LISP早于几十年前面向对象编程的发展。 然而,在后期将对象引入其中。

定义类

defclass宏允许创建用户定义的类。 它将类建立为数据类型。 它具有以下语法 -

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

插槽是存储数据或字段的变量。

插槽描述的格式为(slot-name slot-option *),其中每个选项都是一个关键字,后跟名称,表达式和其他选项。 最常用的插槽选项是 -

  • :accessor函数名称

  • :initform表达式

  • :initarg符号

例如,让我们定义一个Box类,它有三个插槽长度,宽度和高度。

(defclass Box () 
   (length 
   breadth 
   height)
)

为插槽提供访问和读/写控制

除非插槽具有可以访问,读取或写入的值,否则类非常无用。

您可以在定义类时为每个槽指定accessors 。 例如,参加我们的Box课程 -

(defclass Box ()
   ((length :accessor length)
      (breadth :accessor breadth)
      (height :accessor height)
   )
)

您还可以为读取和写入插槽指定单独的accessor名称。

(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}*)

例子 (Example)

让我们创建一个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

继承 (Inheritance)

LISP允许您根据另一个对象定义对象。 这称为inheritance. 您可以通过添加新的或不同的功能来创建派生类。 派生类继承父类的功能。

以下示例解释了这一点 -

例子 (Example)

创建一个名为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
↑回到顶部↑
WIKI教程 @2018