当前位置: 首页 > news >正文

Lua实现链表(面向对象应用)

Lua实现面向对象

  • 面向对象核心三要素
  • Lua面向对象大致原理
    • 面向对象示例
    • 继承与多态示例

面向对象核心三要素

1.封装:对一个事物的抽象为一些属性和行为动作的集合,封装将属性和行为动作(操作数据的方法)绑定在一起,并隐藏对象的内部实现细节,只暴露给外部部分接口。
2. 继承是一种机制,允许一个类(子类)继承另一个类(父类)的属性和方法,从而实现代码重用和扩展。
3. 多态允许一个接口或方法在不同类的实例上有不同的表现形式。通过多态,可以编写更通用、更灵活的代码。

Lua面向对象大致原理

在 Lua 中,面向对象编程(OOP)的概念是通过表(table)和元表(metatable)来实现的。Lua 并没有内建的类系统,但通过灵活的元表机制,可以实现类、继承和多态等 OOP 特性。

面向对象示例

-- 下面通过实现一个简易的链表功能,来展示Lua实现面向对象的大致过程
local Node = {}
Node.__index = Node
Node.new = function(value)return setmetatable({value = value,next = nil},Node)
endlocal LinkList = {}
LinkList.__index = LinkList
LinkList.new = function()return setmetatable({head = nil},LinkList)
end
function LinkList:Insert(value)local node = Node.new(value)if not self.head thenself.head = nodeelselocal curNode = self.headwhile curNode.next docurNode = curNode.nextendcurNode.next = nodeend
endfunction LinkList:InsertByTable(valuetbl)for k,v in ipairs(valuetbl) dolocal node = Node.new(v)if not self.head thenself.head = nodeelselocal curNode = self.headwhile curNode.next docurNode = curNode.nextendcurNode.next = nodeendend
endfunction LinkList:Print()if not self.head thenprint("List has no node")elselocal curNode = self.headwhile curNode doprint("Cur Node Value:",curNode.value)curNode = curNode.nextendend
endfunction LinkList:Reverse()if not self.head thenprint("List has no node")elselocal preNode = nillocal curNode = self.headwhile curNode dolocal nextNode = curNode.nextcurNode.next = preNodepreNode = curNodecurNode = nextNodeendself.head = preNodeend
endlocal l = LinkList.new()
--l:Insert(2)
--l:Insert(4)
--l:Insert(5)
--l:Insert(1)
--l:Insert(0)
l:InsertByTable({1,2,3,4,"a"})
l:Print()
print("---------------------")
l:Reverse()
l:Print()

继承与多态示例

-- 定义一个基类
local Shape = {}
Shape.__index = Shapefunction Shape:new()local instance = setmetatable({}, self)return instance
endfunction Shape:area()return 0
end-- 定义一个子类,继承自 Shape
local Rectangle = setmetatable({}, Shape)
Rectangle.__index = Rectanglefunction Rectangle:new(width, height)local instance = Shape.new(self)instance.width = widthinstance.height = heightreturn instance
endfunction Rectangle:area()return self.width * self.height
end-- 定义另一个子类,继承自 Shape
local Circle = setmetatable({}, Shape)
Circle.__index = Circlefunction Circle:new(radius)local instance = Shape.new(self)instance.radius = radiusreturn instance
endfunction Circle:area()return math.pi * self.radius ^ 2
end-- 创建子类的实例,并展示多态行为
local shapes = {Rectangle:new(3, 4), Circle:new(5)}for _, shape in ipairs(shapes) doprint("Area:", shape:area())  -- 分别输出矩形和圆的面积
end
http://www.lryc.cn/news/388935.html

相关文章:

  • 每隔一个小时gc一次的问题
  • VBA数据库解决方案第十二讲:如何判断数据库中数据表是否存在
  • 五、Spring IoCDI ★ ✔
  • 计算机网络八股文
  • 科普文:一文搞懂jvm原理(四)运行时数据区
  • 《昇思25天学习打卡营第5天|数据变换 Transforms》
  • 详细分析Oracle修改默认的时间格式(四种方式)
  • 以 Vue 3 项目为例,你是否经常遇到 import 语句顺序混乱的问题?要想解决它其实很容易!
  • mysql数据库ibdata文件被误删后恢复数据的方法
  • eBPF技术揭秘:DeepFlow如何引领故障排查,提升运维效率
  • C++视觉开发 三.缺陷检测
  • 使用 Amazon Bedrock Converse API 简化大语言模型交互
  • 第二十一章 函数(Python)
  • 使用pyqt5编写一个七彩时钟
  • 【Linux】:命令行参数
  • 高考假期预习指南,送给迷茫的你
  • 独孤思维:负债了,还可以翻身吗
  • SwiftUI八与UIKIT交互
  • RedHat9 | 内部YUM本地源服务器搭建
  • 无偏归一化自适应心电ECG信号降噪方法(MATLAB)
  • AI基本概念(人工智能、机器学习、深度学习)
  • LabVIEW幅频特性测试系统
  • 校园卡手机卡怎么注销?
  • logback自定义规则脱敏
  • 高效批量复制与覆盖:一键实现文件管理,轻松应对同名文件,简化工作流程
  • vue3中使用Antv G6渲染树形结构并支持节点增删改
  • 【PB案例学习笔记】-26制作一个带浮动图标的工具栏
  • 反向沙箱技术:安全隔离上网
  • 前端在for循环中使用Element-plus el-select中的@click.native动态传参
  • Oracle SQL - CONNECT BY语句Where条件中不能使用OR?[已解决]