Lua现实类 继承 多继承 阶梯式继承 多态

1.Lua类的实现–官方做法

原理1:lua通过表(table)实现类的结构
原理2:lua在通过元表的方式实现继承

--定义父类obj
obj = {id = 0} --定义一个元素id

--通过new方法创建对象
function obj:new(o)
    o = o or {} 
    setmetatable(o,self) --设置obj是o的原表
    self.__index = self  --设置查找元方法
    -- 对象o调用不存在的成员时都会去self(obj)中查找,而这里的self指的就是Object
	return o
end
function obj:Add()
	return 1+1
end

--以下我们创建对象来测试以下
local o1 = obj:new()
function o1:Sub()
	return 10-5
end
o1.id = 11;
--验证结果
print(o1.id)
print(o1:Add())
print(o1:Sub())

2.cocos 使用class 实现继承

1.先创建一个脚本 function.lua 实现下面方法
local setmetatableindex_
setmetatableindex_ = function(t, index)
    if type(t) == "userdata" then
        local peer = tolua.getpeer(t)
        if not peer then
            peer = {}
            tolua.setpeer(t, peer)
        end
        setmetatableindex_(peer, index)
    else
        local mt = getmetatable(t)
        if not mt then mt = {} end
        if not mt.__index then
            mt.__index = index
            setmetatable(t, mt)
        elseif mt.__index ~= index then
            setmetatableindex_(mt, index)
        end
    end
end
setmetatableindex = setmetatableindex_

--参数一:所要创建的类名,参数二:可选参数,可以使function,也可以是table,userdata等
function class(classname, ...) 
    local cls = {__cname = classname}

    local supers = {...}
    for _, super in ipairs(supers) do --遍历可选参数
        local superType = type(super)
        assert(superType == "nil" or superType == "table" or superType == "function",string.format("class() - create class \"%s\" with invalid super class type \"%s\"",classname, superType))
        if superType == "function" then
            assert(cls.__create == nil,string.format("class() - create class \"%s\" with more than one creating function",classname));
            cls.__create = super ----如果是个function,那么就让cls的create方法指向他
        elseif superType == "table" then ----如果是个table
            if super[".isclass"] then ----如果是个原生cocos类,比如cc.Sprite,不是自定义的
                -- super is native class
                assert(cls.__create == nil,string.format("class() - create class \"%s\" with more than one creating function or native class",classname));
                cls.__create = function() return super:create() end
            else
                -- 如果是个纯lua类,自己定义的那种,比如a={}
                cls.__supers = cls.__supers or {}
                cls.__supers[#cls.__supers + 1] = super
                if not cls.super then
                    -- 把第一个遍历到的table作为cls的超类
                    cls.super = super
                end
            end
        else
            error(string.format("class() - create class \"%s\" with invalid super type",classname), 0)
        end
    end

    cls.__index = cls
    if not cls.__supers or #cls.__supers == 1 then --这个就是单继承,设置cls的元表的index为他的第一个超类
        setmetatable(cls, {__index = cls.super})
    else
        setmetatable(cls, {__index = function(_, key) --羡慕是多继承,index指向一个函数,到时候找元素的时候会遍历函数
            local supers = cls.__supers
            for i = 1, #supers do
                local super = supers[i]
                if super[key] then return super[key] end
            end
        end})
    end

    if not cls.ctor then
        -- 增加一个默认构造函数
        cls.ctor = function() end
    end
    cls.new = function(...)
        local instance
        if cls.__create then
            instance = cls.__create(...)
        else
            instance = {}
        end
        setmetatableindex(instance, cls)
        instance.class = cls
        instance:ctor(...)
        return instance
    end
    cls.create = function(_, ...)
        return cls.new(...)
    end
    return cls
end
2.实现过程 创建GameUserItem.lua文件
GameUserItem = class("GameUserItem")
--游戏用户数据模板
function GameUserItem:ctor()
	self.dwGameID		= nil			--游戏(服务器)ID
	self.dwUserID		= nil			--用户ID

	self.wFaceID 		= nil			--头像标识
	self.dwCustomID		= nil			--自定义头像

	self.cbGender		= nil			--性别
	self.cbMemberOrder	= nil			--成员排序
	
	self.szNickName =nil				--用户昵称
	self.szSign=nil						--个性签名
	self.szAddress=nil					--联系地址
end
return GameUserItem
3.创建对象
	require ('Hall.GameModels.GameUserItem')--这里根据你的真实路径来require
	local userItem = GameUserItem:create() --创建对象
	userItem.dwGameID=1000
	userItem.szNickName='张三'
	--打印结果
	print(userItem.dwGameID)
	print(userItem.szNickName)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值