Class programming code instances in Lua _lua

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.