Outline
As you may know we are the object properties and methods of composition, use lua to describe an object, it is bound to have these two characteristics, properties and methods. The basic structure of a lua table, so Lua class, in fact table, because it can store common variables and methods can be stored, we can use table describes the properties and methods of an object.
Objects
In fact, lua To simulate an object, the key lies in setting yuan __index table this index, it is mainly how to do play after the index fail if it points to a table, then the index after __index fails, it will go into this table Finding no specific function or a member of your value, if any, __ index method returns the return value of the function is called.
--- --- 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
Such a lua object now created. Marx and the education of our practice is the sole criterion for testing truth, so the next write a calculator demo we use the object-oriented thinking of writing lua point of view is how inheritance and polymorphism.
addition
Just inherited class writing written Class class, there is x, y value, allows multiple, and so on, we subtraction, multiplication, division
--- --- Generated by EmmyLua(https://github.com/EmmyLua) --- Created by hwc --- DateTime: 2019/12/10 10:17 --- require("operation.Operation") The AddClass = {} - Set membered table Class setmetatable (the AddClass, Class) .__ index the AddClass = the AddClass function the AddClass: new new (X, Y) local Self = {} - call the constructor of the superclass Self = Class: new new (X, Y) setmetatable (Self, the AddClass) return Self End - rewritable function parent function the AddClass: the getResult () return self.x + self.y End
Subtraction
--- --- 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
multiplication
--- --- 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
division
--- --- 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
Polymorphism
We create a class factory management, management of all operations. Thus, if we want to add a root or square opening run, as long as the root class and add code to modify the plant, on the other intact
--- --- 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 = and 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
Adding a Main class to run
to sum up
The use of object-oriented thinking of a lua simple calculator is complete, we simulated the slaughter Lua classes, inheritance, and polymorphism characteristics, played a reusable, scalable, decoupling features for web development projects with lua to a great convenience