Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Emo_Tiny
ZFramework
提交
9f5317a3
Z
ZFramework
项目概览
Emo_Tiny
/
ZFramework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Z
ZFramework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9f5317a3
编写于
8月 13, 2021
作者:
Emo_Tiny
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lua框架底层提交
上级
29897500
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
78 addition
and
2 deletion
+78
-2
Assets/Script/Xlua/LuaScript/LuaClass.lua
Assets/Script/Xlua/LuaScript/LuaClass.lua
+78
-2
未找到文件。
Assets/Script/Xlua/LuaScript/LuaClass.lua
浏览文件 @
9f5317a3
...
...
@@ -26,8 +26,84 @@ local _mtBaseClass =
__tostring
=
_Tostring
}
--实例化
local
function
_Instantiate
(
self
,
...
)
assert
(
_IsA
(
self
,
"class"
),
"Class must be called from a class"
)
assert
(
not
_registry
.
class
[
self
].
__abstract
,
"Can not Instantiate from an abstract class"
)
local
instance
=
_DeepCopy
[
self
]
_registry
.
object
[
instance
]
=
{
__type
=
"object"
,
__superClass
=
self
,
__addr
=
tostring
(
instance
)
}
setmetatable
(
instance
,
{
__index
=
self
,
-- 提供gc时的析构调用,由于gc时机不定,所以不要依赖此方法做资源释放
__gc
=
function
(
obj
)
if
_IsA
(
obj
,
"object"
)
and
obj
.
__dtor
then
obj
:
__dtor
()
end
end
,
__tostring
=
self
.
__tostring
,
--__newindex = function(t, k, v) logerror(string.format("'%s' is not exist in %s", k, t)) end
})
-- 构造调用
if
self
.
__ctor
then
self
.
__ctor
(
instance
,
...
)
end
return
instance
end
--用于判断一个数据的类型,一般用于判断一个table是否是class还是object
local
function
_IsA
(
thing
,
kind
)
if
kind
then
assert
(
kind
==
"object"
or
kind
==
"class"
,
"when given,string 'kind' must be a class or an 'object'"
)
end
if
thing
then
if
_registry
.
class
[
thing
]
then
if
kind
then
return
_registry
.
class
[
thing
].
__type
==
kind
else
return
_registry
.
class
[
thing
].
__type
end
elseif
_registry
.
object
[
thing
]
then
return
_registery
.
object
[
thing
].
__type
==
kind
else
return
_registery
.
object
[
thing
].
__type
end
end
return
false
end
--深拷贝,针对于luaTable,一般也即为类
local
function
_DeepCopy
(
t
)
local
r
=
{}
for
k
,
v
in
pairs
(
t
)
do
local
vtype
=
type
(
v
)
if
vtype
==
"table"
then
if
_registry
.
class
[
v
]
or
_registry
.
object
[
v
]
then
r
[
k
]
=
v
else
_DeepCopy
(
v
)
end
elseif
(
vtype
~=
"function"
)
then
r
[
k
]
=
v
end
end
end
--普通继承
local
function
_Extends
(
self
,
name
,
flags
)
assert
(
_IsA
(
self
,
"class"
),
"Inheritance must be called from a class"
)
assert
(
not
_registry
.
class
[
self
].
__final
,
"Can not derive from a final class"
)
local
class
=
_CreateClass
(
name
,
flags
)
class
.
__index
=
class
class
.
__tostring
=
_ToString
_registry
.
class
[
class
].
__superClass
=
self
_registry
.
class
[
self
].
__subClass
=
class
return
setmetatable
(
class
,
self
)
end
function
_CreateClass
(
name
,
flags
)
...
...
@@ -40,8 +116,8 @@ function _CreateClass(name,flags)
__name
=
name
,
__abstract
=
flags
and
flags
.
abstract
or
false
,
__final
=
flags
and
flags
.
final
or
false
,
__superClass
=
false
,
__subClass
=
setmetatable
({},{
__mode
=
"k"
}),
__superClass
=
false
,
--父类
__subClass
=
setmetatable
({},{
__mode
=
"k"
}),
--子类
__addr
=
tostring
(
newClass
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录