- LISP 教程
- LISP - 首页
- LISP - 概述
- LISP - 环境
- LISP - 程序结构
- LISP - 基本语法
- LISP - 数据类型
- LISP - 宏
- LISP - 变量
- LISP - 常量
- LISP - 运算符
- LISP - 决策
- LISP - 循环
- LISP - 函数
- LISP - 谓词
- LISP - 数字
- LISP - 字符
- LISP - 数组
- LISP - 字符串
- LISP - 序列
- LISP - 列表
- LISP - 符号
- LISP - 向量
- LISP - 集合
- LISP - 树
- LISP - 哈希表
- LISP - 输入与输出
- LISP - 文件 I/O
- LISP - 结构体
- LISP - 包
- LISP - 错误处理
- LISP - CLOS
- LISP 有用资源
- Lisp - 快速指南
- Lisp - 有用资源
- Lisp - 讨论
LISP - CLOS
Common LISP 的出现比面向对象编程早了几十年。然而,面向对象特性在后期被整合到其中。
定义类
defclass 宏允许创建用户自定义类。它将类建立为一种数据类型。其语法如下:
(defclass class-name (superclass-name*) (slot-description*) class-option*))
槽是存储数据的变量,或字段。
槽描述符的形式为 (slot-name slot-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 的新源代码文件,并在其中键入以下代码。
(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
广告