How to implement class inheritance?

I want to script a base class and then script other class to inherit the base class. for example, there is a class of monster, and some diffently derive monster class. but in Core, I don't find some function like global function named class in Cocos2d-lua Engine.in total, how to extend a base class ?

I have not used lua very much, but here is a possible approach. If anyone has a better one, I would be happy to see it!

Create a script called Class_Monster_Base

local MONSTER_BASE = {}
MONSTER_BASE.__index = MONSTER_BASE

function MONSTER_BASE:new ()
    local self = setmetatable({}, MONSTER_BASE)
    self.hitpoints = 9
    return self
end

function MONSTER_BASE:SetHitpoints(value)
    self.hitpoints = value
end

function MONSTER_BASE:GetHitpoints()
    return self.hitpoints
end

return MONSTER_BASE

Delete it from your scene hierarchy to avoid confusion (you will still be able to see it in Project Content - My Scripts).

Create a script called Class_Monster_Extended

local MONSTER_BASE = require(script:GetCustomProperty("BaseClass"))

local EXTENDED_MONSTER = {}

function EXTENDED_MONSTER:new ()
    local self = MONSTER_BASE:new()
    self.ac = 5
    return self
end

function MONSTER_BASE:GetAC()
    return self.ac
end

return EXTENDED_MONSTER

Delete it from your scene hierarchy to avoid confusion (you will still be able to see it in Project Content - My Scripts).

Highlight Class_Monster_Extended in Project Content - My Scripts and under its properties + Add Custom Property, Asset Reference, call it BaseClass and drag the Class_Monster_Base script from Project Content - My Scripts into it.

Create a script called MyScript

local EXTEND_CLASS = require(script:GetCustomProperty("ExtendClass"))
local BASE_CLASS = require( script:GetCustomProperty("BaseClass") )

local newMonster1 = BASE_CLASS:new()
local newMonster2 = BASE_CLASS:new()

newMonster2:SetHitpoints(7)

print(newMonster1:GetHitpoints())
print(newMonster2:GetHitpoints())

local ext = EXTEND_CLASS:new()

print("Extended HP: " .. ext:GetHitpoints())
print("Extended AC: " .. ext:GetAC())

For MyScript + Add Custom Property, Asset Reference for ExtendClass and BaseClass and drag the scripts in from Project Content - My Scripts.

Leave this in your hierarchy and try it out. Note MyScript does not have to reference the base class asset in order to use the extended class asset. You can try this out by commenting bits out / deleting the base class asset reference. I just put both in to demonstrate the class and its extension in the single script.

I find a approach to resolve this problem.see below.
-- Utils.lua

function Utils.Clone(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for key, value in pairs(object) do
            new_table[_copy(key)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
    end
    return _copy(object)
end

local objectCount = 0
local function getObjectCount()
    objectCount = objectCount + 1
    return objectCount
end

function Utils.Class(classname, super)
    local superType = type(super)
    local cls

    if superType ~= "function" and superType ~= "table" then
        superType = nil
        super = nil
    end

    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[k] = v end
            cls.__create = super.__create
            cls.super    = super
        else
            cls.__create = super
            cls.ctor = function() end
        end

        cls.__cname = classname
        cls.__ctype = 1

        function cls:new(...)
            local instance = cls.__create(...)
            instance.id = getObjectCount()
            instance.class = cls
            instance:ctor(...)
            return instance
        end

    else
        -- inherited from Lua Object
        if super then
            cls = Utils.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 = {}
            instance.id = getObjectCount()
            instance.class = cls
            setmetatable(instance, cls)
            instance:ctor(...)
            return instance
        end
    end

    function cls:create(...)
        return cls:new(...)
    end
    
    return cls
end

-- base.lua

local utils = require("71D78F4310179502:Utils")
local BASE_CLASS = utils.Class("BASE_CLASS")
function GameManagerServer:ctor()
   self.testNumber = 0
end
function BASE_CLASS:print()
   print(self.testNumber)
end
return BASE_CLASS

--extend.lua

local utils = require("71D78F4310179502:Utils")
local BASE = require("XXXXXXXXXXXXXXX:base")
local EXTEND_CLASS = utils.Class("EXTEND_CLASS",BASE)
function EXTEND_CLASS:ctor()
   self.testNumber = 100
end

function  EXTEND_CLASS:print()
   print(self.testNumber)
end
return BASE_CLASS

-- test.lua

local base = require("xxxxxx:base").new() //--require("xxxxxx:base").create()
base.print()  //::0
local extend = require("xxxxxx:extend").new() //--require("xxxxxx:extend").create()
extend.print() //:: 100

This approach is my exp from cocos2d-lua engine. Hope to get advice. it is easy to help us resolve object-oriented when game has a variety of monsters have a same logic.