Lua class inheritance and implementation

Source: Internet
Author: User

http://blog.csdn.net/ssihc0/article/details/7742323

Account={balance=0}; --Create a new pair like, he has an attribute Balancefunction account:New(o)--here:New(o) in the colon, the code can omit self, at the time of the Access object:New(o) If it is a point number:New(o) should be changed.New(Self,o) at the time of the visitObject. (Object, O) o=o or {} setmetatable (o,self);--O inherit from self self.__index=self;--Associated meta-table entriesreturno;endfunction account.deposit (self,v) self.balance=self.balance+v;endfunction Account:withdraw (v)if(v) > self.balance then Error"Insufficient funds"; end Self.balance=self.balance-V;endspecialaccount=account:New(); --create ANew Object class, Basic accountclasss=specialaccount:New{limit=1000.00};--instantiate a new Specialaccount object print (s.balance); S:deposit (100.00); function Specialaccount:withdraw (v)ifV-self.balance >=self:getlimit () then error"Insufficient funds"; End Self.balance=self.balance-v;endfunction specialaccount.getlimit (self)returnSelf.limit or0; EndPrint (s.limit);p rint (S.getlimit (s)) print (s.balance)

Lua class inheritance and implementation

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.