1.lua一个类A继承于C++一个对象,如cc.Layer:create();
2.类A重写自己的构造函数A:ctor();
3.类B继承于类A,但是不会继承于类A的构造函数。因为在extern文件的class方法中,把类A当做了C++的对象,并且有一句cls.ctor = function() end,将ctor方法置为了空方法
应该判断一下,如果类A有构造函数,则不重写。或者类A应该属于Lua对象而不是C++对象
if superType == "function" or (super and super.__ctype == 1) then
-- inherited from native C++ Object
cls = {}
if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
end
cls.ctor = function() end
cls.__cname = classname
cls.__ctype = 1
function cls.new(...)
local instance = cls.__create(...)
-- copy fields from class to native object
for k,v in pairs(cls) do instance = v end
instance.class = cls
instance:ctor(...)
return instance
end
else
-- inherited from Lua Object
if super then
cls = clone(super)
cls.super = super
else
cls = {ctor = function() end}
end
cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls
function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end
```