首页 > 其他分享 >无涯教程-LISP - 类(defclass)

无涯教程-LISP - 类(defclass)

时间:2024-01-14 15:31:49浏览次数:17  
标签:box Box breadth LISP 无涯 height item length 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类为示例-

(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

参考链接

https://www.learnfk.com/lisp/lisp-clos.html

标签:box,Box,breadth,LISP,无涯,height,item,length,defclass
From: https://blog.51cto.com/u_14033984/9240883

相关文章

  • 无涯教程-LISP - 软件包(Packages)
    用编程语言来说,程序包旨在提供一种使一组名称彼此分开的方法,在一个程序包中声明的符号不会与在另一个程序包中声明的相同符号冲突,这样,程序包减少了独立代码模块之间的命名冲突。当前包由特殊变量*package*引用。LISP中有两个预定义的程序包-common-lisp      -......
  • 无涯教程-LISP - 文件I/O
    在本章中,我们将了解LISP如何创建,打开,关闭文本或二进制文件进行数据存储。打开文件您可以使用open函数来创建新文件或打开现有文件,with-open-file通常更方便,更常用,这将在本节的后面看到。打开文件后,将在LISP环境中构造一个流对象来表示它。open函数的语法是-openfilename......
  • 无涯教程-LISP - 输入&输出
    常见的LISP提供许多输入输出函数,我们已经使用了格式化函数和打印函数进行输出,在本节中,我们将研究LISP中提供的一些最常用的输入输出函数。Read函数下表提供了LISP最常用的输入函数-Sr.No.Function&描述1read&optionalinput-streameof-error-peof-valuerecursive-p......
  • 无涯教程-LISP - 集合(Set)
    adjoin函数首先在给定列表中查找该元素(如果找到),然后返回原始列表,否则,它将创建一个新的cons单元格,其car作为元素,而cdr指向原始列表,并返回此新列表。adjoin函数还使用:key和:test关键字参数。adjoin函数不会修改原始列表,因此要更改列表本身,您必须将adjoin返回的值分......
  • 无涯教程-LISP - 数字(Numbers)
    CommonLisp number数据类型包括LISP支持的各种数字。LISP支持的数字类型是-IntegerRatiosFloatComplex下图显示了LISP中可用的数字层次结构和各种数字数据类型-数字类型下表描述了LISP中可用的各种数字类型数据-Sr.No.Datatype&描述1fixnum此数据类型表示......
  • 无涯教程-LISP - 函数声明
    函数是一起执行任务的一组语句。定义函数名为defun的宏用于定义函数,defun宏需要三个参数-函数名称函数参数函数主体defun的语法是-(defunname(parameter-list)"Optionaldocumentationstring."body)让我们用简单的示例来说明这个概念。函数-示例1让我们编写......
  • 无涯教程-LISP - 循环语句
    在某些情况下,您需要执行一段代码次数,循环语句使我们可以多次执行一个语句或一组语句,以下是大多数编程语言中循环语句的一般形式。LISP提供以下类型的构造来处理循环需求。单击以下链接以查看其详细信息。Sr.No.Construct&描述1loop以最简单的形式,它允许您重复执行某些语......
  • 无涯教程-LISP - 条件判断
    以下是大多数编程语言中常见的典型决策结构的一般形式-LISP提供以下类型的决策构造,单击以下链接以查看其详细信息。Sr.No.Construct&描述1cond此构造用于检查多个判断操作子句。2ifif构造具有多种形式。3when如果test子句的判断输出为true,则执行test操作,否则,对后......
  • 无涯教程-LISP - 运算符
    运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。LISP允许对数据进行大量操作,并由各种函数,宏和其他构造支持。允许对数据进行的操作可以归类为-算术运算比较操作逻辑运算按位运行算术运算下表显示了LISP支持的所有算术运算符。假设变量A=10,变量B=20,然后-运算符......
  • 无涯教程-LISP - 常量声明
    在LISP中,常量是在程序执行期间永不更改其值的变量,常量使用defconstant构造进行声明。以下示例显示了声明全局常量PI并随后在名为area-circle的函数中使用此值来计算圆的面积的情况。defun构造用于定义一个函数,我们将在Function一章中对其进行研究。创建一个名为main.l......