咱們總所周知對象是由屬性和方法組成的,要用lua要描述一個對象,也必然要有這兩個特性,屬性和方法。lua的基本結構是table,因此Lua的類,其實都是table,由於它能夠存儲普通的變量又能夠存儲方法,咱們利用table就能夠描述一個對象的屬性和方法。git
其實lua要模擬一個對象,關鍵就在於__index設置元表索引這塊,它主要起到索引失敗後該怎麼辦,若是它指向一張表,那麼__index索引失敗後,它會到這張表中去查找有沒有你指定的函數或成員值,若是有,__index方法被調用時會返回該函數的返回值。github
--- --- 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來看下是怎麼實現繼承與多態。web
繼承剛纔寫得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
咱們建立一個管理工廠類,管理全部運算。這樣一來咱們若是要在添加一個開根號或者平方運行,只要在添加根號類和修改工廠的代碼,其餘就原封不動ui
--- --- 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項目帶來了極大的便利spa