The Lua class is a bit like JavaScript, but more concise and flexible, and the table is the object, and the object is the class. Metatables is easier to use than Ruby Metaclass, and the downside is that there's a lot of instantiation and inheritance code,
Unlike Ruby's "<" and "<<", the inheritance chain is the chain of methods that are used to find the method.
account={test1=function (a) print (' Account test1 ') end} account.test2=function (a) print (' Account test2 ') End Functio
n Account.test3 (a) print (' Account test3 ') End Function Account:new (o)--instantiation of class o = O or {} setmetatable (O, self)
Self.__index = self return o-End Function account.print0 (o,a) print (a) End Function Account:print1 (a) print (a) End--Method definition Test Account.test1 () Account.test2 () Account.test3 ()-Class Test Acc=account:new () Acc.test1 () acc.print0 (ACC, "Dot p Rint0 ") acc:print0 (" Not dot print0 ") acc.print1 (ACC," dot print1 ") acc:print1 (" Not dot print1 ") acc.specialmethod= function (specialmethodtest) print (specialmethodtest) End Acc.specialmethod ("SMT Test")--Inheritance Test Specialaccount = Account : New () s = specialaccount:new{limit=1000.00}--Multiple inheritance Test Named = {} function Named:getname () return Self.name end Fun Ction named:setname (n) self.name = N End Local function search (k, plist) for I=1, TABLE.GETN (plist) do Loca L v = plist[i][k] If V then Return v-end-End Function Createclass (...)
Local c = {}--The new class setmetatable (c, {__index = function (t, K) return search (k, arg) end}) C.__index = C function C:new (o) o = O or {} setmetatable (o, c) return o ' return c-end namedaccount = Createclass (accou
NT, Named) account = Namedaccount:new{name = "Paul"} print (Account:getname ())