📅  最后修改于: 2020-11-03 07:22:39             🧑  作者: Mango
常见的LISP早于几十年的面向对象编程的发展。但是,它在稍后的阶段将面向对象并入其中。
defclass宏允许创建用户定义的类。它建立一个类作为数据类型。它具有以下语法-
(defclass class-name (superclass-name*)
(slot-description*)
class-option*))
插槽是存储数据或字段的变量。
插槽说明的格式为(slot-name slot-option *),其中每个选项都是关键字,后跟名称,表达式和其他选项。最常用的插槽选项是-
: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