Lua的面向对象,封装,继承,多态

wqiaofujiang 2019-12-10

概述

我们总所周知对象是由属性和方法组成的,要用lua要描述一个对象,也必然要有这两个特性,属性和方法。lua的基本结构是table,所以Lua的类,其实都是table,因为它可以存储普通的变量又可以存储方法,我们利用table就可以描述一个对象的属性和方法。

对象

其实lua要模拟一个对象,关键就在于__index设置元表索引这块,它主要起到索引失败后该怎么办,如果它指向一张表,那么__index索引失败后,它会到这张表中去查找有没有你指定的函数或成员值,如果有,__index方法被调用时会返回该函数的返回值。

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hwc
--- DateTime: 2019/12/10 10:14
---
Class = {x,y}
Class.__index = Class
function Class:new(x,y)
    local self = {}
    setmetatable(self,Class)
    self.x = x
    self.y = y
    return self
end
function Class:getResult()
    return 0
end

这样一个lua的对象就创建完成了。马克思教育我们实践是检验真理的唯一标准,所以我们用lua的写面向对象的思想写一个计算器的demo来看下是怎么实现继承与多态。

加法

继承刚才写得Class类,就有了x,y值,可以进行加法运算,以此类推,我们进行减法,乘法,除法的类编写

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hwc
--- DateTime: 2019/12/10 10:17
---
require("operation.Operation")
AddClass = {}
--设置元表为Class
setmetatable(AddClass,Class)
AddClass.__index = AddClass
function AddClass:new(x,y)
    local self = {}
    --调用父类的构造函数
    self = Class:new(x,y)
    setmetatable(self,AddClass)
    return self
end
--重写父类函数
function AddClass:getResult()
    return self.x + self.y
end

减法

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hwc.
--- DateTime: 2019/12/10 10:35
---
require("operation.Operation")
SubClass = {}
setmetatable(SubClass,Class)
SubClass.__index = SubClass
function SubClass:new(x,y)
    local self = {}
    self = Class:new(x,y)
    setmetatable(self,SubClass)
    return self
end
function SubClass:getResult()
    return self.x - self.y
end

乘法

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hwc.
--- DateTime: 2019/12/10 10:39
---
require("operation.Operation")
MulClass = {}
setmetatable(MulClass,Class)
MulClass.__index = MulClass
function MulClass:new(x,y)
    local self = {}
    self = Class:new(x,y)
    setmetatable(self,MulClass)
    return self
end
function MulClass:getResult()
    return self.x * self.y
end

除法

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hwc.
--- DateTime: 2019/12/10 10:42
---
require("operation.Operation")
DivClass = {}
setmetatable(DivClass,Class)
DivClass.__index = DivClass
function DivClass:new(x,y)
    local self = {}
    self = Class:new(x,y)
    setmetatable(self,DivClass)
    return self
end
function DivClass:getResult()
    if self.y == 0 then
        print("除数不能为零")
        return -1
    end
    return self.x / self.y
end

多态

我们创建一个管理工厂类,管理所有运算。这样一来我们如果要在添加一个开根号或者平方运行,只要在添加根号类和修改工厂的代码,其他就原封不动

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hwc.
--- DateTime: 2019/12/10 10:46
---
require("operation.OperationAdd")
require("operation.OperationSub")
require("operation.OperationMul")
require("operation.OperationDiv")
FactoryClass = {}
FactoryClass.__index = FactoryClass
function FactoryClass:new(x,y,z)
    local self = {}
    setmetatable(self,FactoryClass)
    self.x = x
    self.y = y
    self.z = z
    return self
end
function FactoryClass:result()
    local myTable = {}
    if self.z == "+" then
        myTable = AddClass:new(self.x,self.y)
        return myTable
    elseif self.z == "-" then
        myTable = SubClass:new(self.x,self.y)
        return myTable
    elseif self.z == "*" then
        myTable = MulClass:new(self.x,self.y)
        return myTable
    elseif self.z == "/" then
        myTable = DivClass:new(self.x,self.y)
        return myTable
    end
end

添加一个Main类运行

Lua的面向对象,封装,继承,多态

 总结

一个利用面向对象思想的lua简单计算器就完成了,我们宰Lua中模拟了类,继承,和多态的特征,起到了可复用,可扩展,解耦的特性,为lua开发web项目带来了极大的便利

 

相关推荐