Lisp在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
做个小测试, 发布于:2024-03-14 09:53 桥梁安全计算lisp 发布于:2023-10-24 09:36 定义函数 newCalc,接受两个参数 x 和 y 发布于:2023-09-23 16:06 判断两数之和大于第三个数 发布于:2023-09-01 16:21 分数陈述性知识 发布于:2023-08-24 22:27 CAD面积/体积计算 发布于:2023-04-19 12:08 测试lisp语言 发布于:2023-04-10 15:03 计算list中某一元素数量 发布于:2023-02-16 11:32 LISP 初体验 发布于:2021-08-11 13:16 XY坐标标注CAD插件 发布于:2021-06-11 15:25 format t 输出语句 发布于:2020-08-10 18:21 print 输出语句 发布于:2020-08-10 18:20 write 输出语句 发布于:2020-08-10 18:16 write-line 输出语句 发布于:2020-08-10 18:14 循环输出内容 发布于:2020-08-10 18:11 [更多]
显示目录

面向对象



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

面向对象

Common Lisp通过几十年的面向对象编程的推进。但是,面向对象被并入是在它最后一阶段。

类的定义

defclass宏允许创建用户定义的类。它建立了一个类作为数据类型。它的语法如下:


(DEFCLASS class\-name (superclass\-name\*)  
(slot\-description\*) 
class\-option\*)

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

slot-description形式(插槽名称插槽选项*),其中每个选项是一个关键字后跟一个名字,表达式和其他选项。最常用的槽选项是:

  • :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一个新的源代码文件,并在其中输入如下代码:


(adsbygoogle = window.adsbygoogle || \[\]).push({});

(adsbygoogle = window.adsbygoogle || \[\]).push({});

(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
由JSRUN为你提供的Lisp在线运行、在线编译工具
        JSRUN提供的Lisp 在线运行,Lisp 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout