Implementation of classes in Lua _lua

Source: Internet
Author: User
Tags inheritance lua

Overview

A class is like a stencil that creates objects, and for Lua, a language with no class concept, the way to simulate classes is to develop a prototype (prototype) for the object to be created. This prototype corresponds to a class in another language. But the prototype is also a regular object, and when other objects (as examples of prototypes) encounter an unknown operation, they look in the prototype. Therefore, in a language that does not have classes in Lua, in order to represent a class, simply create a prototype that is dedicated to other objects. Both classes and prototypes are a way of organizing shared behavior among objects. This article will simulate the class in Lua, and the related code can run directly on my github.

Realize

The key to simulating classes in Lua is the inheritance mechanism of class, and class instantiation process, this process mainly uses the meta table technology and whether to copy the method to subclass or the instance (if copy, increase the data redundancy, and lose the parent class update subclass also will automatically update the attribute, If you do not copy, then each access to the parent class method, because the use of the meta table, the code additional overhead), the following is an implementation method:

Clsobject = { 
__classtype = "class type" 
} 
                                                     
function Clsobject:inherit (o)   
  o = O or {} 
O.__classtype = " Class Type " 
  o.mt = {__index = o} 
  setmetatable (o, {__index = self}) return O-end 
 
function Clsobject: New (...) 
  Local o = {} 
 
  setmetatable (o, self.mt) 
 
  if o.__init__ then 
    o:__init__ (...) 
  End return  
  o 
- 
 
function clsobject:__init__ () 
end 
 
function Clsobject:destroy () 
End 
 
function Clsobject:gettype () return 
  "BaseClass" 
End 

Above, whether in the inheritance mechanism or the instantiation process, are using the Meta table technology, this is consistent with the idea of class inheritance. In addition to the above implementation, you can also implement the tool function: To get a class of the parent class and to determine whether a class is a subclass or whether the object is changed class instances, the code is as follows:

function Super (tmpclass) return 
  getmetatable (tmpclass). __index 
End 
 
function issub (clssub, Clsancestor)  Local 
  temp = clssub While 1 does local 
    MT = getmetatable (temp) 
    if Mt then 
      temp = mt.__index 
      If temp = = Clsancestor then return 
        true ' 
      else return 
      false  
' End end 

You can use this class by following the example code below

Clsparent = Clsobject:inherit () 
 
function Clsparent:foo () 
  print ("Parentfoo!") 
End local 
 
parentobj = clsparent:new () 
parentobj:foo () 
 
 
Clsson = Clsparent:inherit () 
function Clsson:foo () 
  Super (Clsson). Foo (self) 
  print ("Sonfoo") End local 
 
sonobj = Clsson:inherit () 
sonobj:foo () 
 
print ( Clsson, clsparent)) 
print (Issub (Clsson, Clsobject)) 

This is the whole story of this article, and hopefully it will help you to master the Lua script.

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.