LISP - 类(defclass)

LISP - 类(defclass) 首页 / LISP入门教程 / LISP - 类(defclass)

常见的LISP早于几十年的面向对象编程的发展,但是,它在稍后的阶段将面向对象并入其中。

定义类

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

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

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

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

  • :accessor 函数名

  • :initform 表达式

  • :initarg    符号

如,让我们定义一个Box类,它具有三个slot的长度,宽度和高度。

(defclass Box () 
   (length 
   breadth 
   height)
)

读/写控制

定义类时,可以为每个slot指定访问器。如,以我们的Box类为示例-

链接:https://www.learnfk.comhttps://www.learnfk.com/lisp/lisp-clos.html

来源:LearnFk无涯教程网

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

您还可以为读取和写入slot指定单独的访问器名称。

(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类,它具有三个slot,长度,宽度和高度,我们将使用三个slot访问器在这些字段中设置值。

无涯教程网

创建一个名为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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

软件工程之美 -〔宝玉〕

大规模数据处理实战 -〔蔡元楠〕

接口测试入门课 -〔陈磊〕

检索技术核心20讲 -〔陈东〕

Redis源码剖析与实战 -〔蒋德钧〕

零基础实战机器学习 -〔黄佳〕

深入拆解消息队列47讲 -〔许文强〕

工程师个人发展指南 -〔李云〕

好记忆不如烂笔头。留下您的足迹吧 :)