Home > other >  Cocos2dx lua class inheritance
Cocos2dx lua class inheritance

Time:09-25

Engine version, 3.4 final cocos2dx

The default class function is
 function class (classname,... ) 
The local CLS={__cname=classname}

The local supers={... }
For _, super in ipairs (supers) do
The local superType=type (super)
Assert (superType=="nil" or superType=="table" or superType=="function",
The string. Format (" class () - create a class \ "% s " with invalid super class type \ "% s " ",
The classname, superType))

If superType=="function" then
Assert (CLS) __create==nil,
The string. Format (" class () - create a class \ "% s " with wining one creating the function ",
The classname));
- if super is function, set it to __create
CLS. __create=super
Elseif superType=="table" then
If super [". Isclass "] then
- super is native class
Assert (CLS) __create==nil,
The string. Format (" class () - create a class \ "% s " with wining one creating function or native class ",
The classname));
CLS. __create=function () return super: the create () end
The else
- super is pure lua class
CLS. __supers=CLS. __supers or {}
CLS. __supers [# CLS. __supers + 1]=super
If not CLS. Super then
- set the first super pure lua class as a class. The super
CLS. Super=super
End
End
The else
Error (string format (" class () - create a class \ "% s " with invalid super type ",
The classname), 0)
End
End

CLS. Index=CLS
If not CLS. # __supers or CLS. __supers==1 then
Setmetatable (CLS, {index=CLS. Super})
The else
Setmetatable (CLS, {index=function (_, key)
The local supers=CLS. __supers
For I=1, # supers do
The local super=supers [I]
[key] if super then return super [key] end
End
End})
End

If not CLS. Ctor then
- add the default constructor
CLS. Ctor=function () end
End
CLS. New=function (... )
The local instance
If the CLS. __create then
The instance=CLS. __create (... )
The else
The instance={}
End
Setmetatableindex (instance, CLS)
The instance. The class=CLS
Instance: ctor (... )
Return the instance
End
CLS. Create=function (_,... )
The return of CLS. New (... )
End

The return of CLS
End


This function probably three part,
The first paragraph, it is to distinguish the type of the parent, only support table and function as a parent, if the function, the first function as the class of the create function,
The second paragraph, the table is set various yuan index method, so that the parent class element,
The third section, is to set up new methods, to create a new object, mainly involves two kinds, one kind is the create method, one kind is not the create method,

A test is made below, write a parent class:
 local baseClass=class (" base ") 

The function baseClass: ctor ()
Self. BaseNum=1
End

The function baseClass: baseFunc ()
Print (" base Func ".. Self. BaseNum)
End

Return baseClass

Then write a subclass:
 local levelTwo=class (" two ", the require (" res. Role. Tt. HHH. BaseClass ") : the create ()) 

The function levelTwo: ctor ()
Self. TwoNum=100
End

The function levelTwo: twoFunc ()
Print (" two Func, base num: ".. Self. BaseNum)
Print (" two Func, two num: ".. Self. TwoNum)
End

Return levelTwo

Then the entry function test:
 local baseObj=the require (" res. Role. Tt. HHH. BaseClass ") : the create () 
BaseObj: baseFunc ()

The local twoObj=the require (" res. Role. Tt. HHH. LevelTwo ") : the create ()
TwoObj: baseFunc ()
TwoObj: twoFunc ()


This test, the result is normal, can output correctly, if change the subclass to inherited as follows:
 local levelOne=class (" one ", function () 
Return the require (" res. Role. Tt. HHH. BaseClass ") : the create ()
End)

The function levelOne: ctor ()
Self. OneNum=10
End

The function levelOne: oneFunc ()
Print (" one Func, base num: ".. Self. BaseNum)
Print (" one Func, one num: ".. Self. OneNum)
End

Return levelOne

Complains that subclass methods can not find,

Excuse me why
  • Related