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

发布于:2024-07-03 ⋅ 阅读:(16) ⋅ 点赞:(0)

面向对象核心三要素

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)
end

local 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 then
        self.head = node
    else
        local curNode = self.head
        while curNode.next do
            curNode = curNode.next
        end
        curNode.next = node
    end
end

function LinkList:InsertByTable(valuetbl)
    for k,v in ipairs(valuetbl) do
        local node = Node.new(v)
        if not self.head then
            self.head = node
        else
            local curNode = self.head
            while curNode.next do
                curNode = curNode.next
            end
            curNode.next = node
        end
    end
end

function LinkList:Print()
    if not self.head then
        print("List has no node")
    else
        local curNode = self.head
        while curNode do
            print("Cur Node Value:",curNode.value)
            curNode = curNode.next
        end
    end
end

function LinkList:Reverse()
    if not self.head then
        print("List has no node")
    else
        local preNode = nil
        local curNode = self.head
        while curNode do
            local nextNode = curNode.next
            curNode.next = preNode
            preNode = curNode
            curNode = nextNode
        end
        self.head = preNode
    end
end

local 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 = Shape

function Shape:new()
    local instance = setmetatable({}, self)
    return instance
end

function Shape:area()
    return 0
end

-- 定义一个子类,继承自 Shape
local Rectangle = setmetatable({}, Shape)
Rectangle.__index = Rectangle

function Rectangle:new(width, height)
    local instance = Shape.new(self)
    instance.width = width
    instance.height = height
    return instance
end

function Rectangle:area()
    return self.width * self.height
end

-- 定义另一个子类,继承自 Shape
local Circle = setmetatable({}, Shape)
Circle.__index = Circle

function Circle:new(radius)
    local instance = Shape.new(self)
    instance.radius = radius
    return instance
end

function Circle:area()
    return math.pi * self.radius ^ 2
end

-- 创建子类的实例,并展示多态行为
local shapes = {Rectangle:new(3, 4), Circle:new(5)}

for _, shape in ipairs(shapes) do
    print("Area:", shape:area())  -- 分别输出矩形和圆的面积
end