Base classes and "inheritance mechanisms" in Lua

Source: Internet
Author: User

base class : The base class defines all the common properties and methods for derived classes, derived classes inherit the required properties and methods from the base class, and new properties and methods are added to the derived classes.

inheritance : Inheritance is an important mechanism of the C + + language that allows new classes to be generated on the basis of defined classes.

The LUA base class is very similar to the C + + base class, but there is no inheritance in Lua, and there are no so-called derived classes. Lua can only emulate the method of C + + inheritance through a single behavior (a meta-table).

meta table : A meta-table provided in Lua is a personalized behavior that helps LUA data variables complete certain non-predefined functions, and when it does something, but the self table does not define a way to do so, then in order to do this, it will go to the meta-table to find a way to do this.

If each layer of MetaTable defines a method to point to the previous layer to "inherit" the Lua table, so that is not the same as C + + inheritance, there are wood!

meta-method : Inheritance in C + + does not change the general behavior of the language. But Lua provides a way to change table behavior with two types of table behavior that can be changed: (__index meta-method ) query table and ( __newindex ) To modify fields that do not exist in table

(1)__index meta-method: When accessing a field that does not exist in the meta-table, the result is nil. By defining the __index of the meta-table, the access result will be determined by this method.

This method is also the "inherit" method of the parent class.

(2)__newindex meta-method: When assigning a value to a field that does not exist in the meta-table, the interpreter will first find the __newindex of the meta-table, and if so, call it and assign a value to the table that __newindex points to. If not, assign a value to the self table.

1 --to save a virtual table of class type2 Local _class= {}3 4Global_obj_count = {}5Enable_obj_count =06 7 functionfindclassname (target, depth)8      forKey,valueinch Pairs(_g) Do9         ifValue = = Target ThenTen             returnKey One         End A     End - End -  the functionClascountretain (c) -     LocalKey =Findclassname (c) -     ifGlobal_obj_count[key] = =Nil  Then -Global_obj_count[key] =1 +     Else -Global_obj_count[key] = Global_obj_count[key] +1 +     End A End at  - functionClascountrelease (c) -     LocalKey =Findclassname (c) -     ifGlobal_obj_count[key] = =Nil  Then -Global_obj_count[key] =-100000--Identifying Exceptions -     Else inGlobal_obj_count[key] = Global_obj_count[key]-1 -     End to End +  -  the  *  $ functionprintluaclasscount (...)Panax Notoginseng     Print("printluaclasscount ........") -      forKey,valueinch Pairs(Global_obj_count) Do the         Print("Printluaclasscount:".. Key:":", value) +     End A End the  +  - functionBaseClass (Super) $  $     --generate a class type -     LocalClass_type = {} -     --automatically called when the object is created theClass_type.__init =false -Class_type.__delete =falseWuyiClass_type.super =Super the  -Class_type. New =function(...)--defining new Member Methods Wu         --generating a Class object -         Localobj = {} AboutObj._class_type=Class_type $  -         --registering a base class method before initializing -         setmetatable(obj, {__index =_class[Class_type]}) -  A         --Call initialization Method +          Do the             LocalCreate -Create =function(c, ...)  $                 ifC.super Then theCreate (C.super, ...)--init for all base classes the                 End the                 ifEnable_obj_count ~=0  Then the Clascountretain (c) -                 End in                 ifC.__init Then the c.__init (obj, ...) the                 End About             End the  the Create (Class_type, ...) the         End +  -         --register a Delete method theObj. DeleteMe =function(self)Bayi             LocalNow_super = self._class_type  the              whileNow_super ~=Nil  Do the                 ifEnable_obj_count ~=0  Then - clascountrelease (now_super) -                 End     the                 ifNow_super.__delete Then theNow_super.__delete (self)--Delete for all base classes the                 End theNow_super =Now_super.super -             End the         End the  the         returnobj94     End the  the     LocalVTBL = {} the     _class[Class_type] =VTBL98  About     setmetatable(Class_type, {__newindex = -         function(t,k,v)101Vtbl[k] = V--The value of the VTBL table is assigned to a field that is not found by self when the assignment operation102         End103         , 104__index = VTBL,--For call Parent method the     })106 107     ifSuper Then108         setmetatable(VTBL, {__index =--The meta table does an "inheritance" Operation109             function(t,k) the                 LocalRET =_class[Super][k]111                 returnret the             End113         }) the     End the  the     returnClass_type117 End
View Code

Base classes and "inheritance mechanisms" in Lua

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: 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.