理解现有的“lua”类并继承它。
2014-5-14 7:7:38
收藏:0
阅读:124
评论:1
我正在尝试创建一个继承别人创建的另一个类的新类,但我不理解这个类的制作方式,也无法弄清如何制作我的新类。
每个类都在单独的“。lua”文件中。
基类看起来像这样:
local Base = {}
local function new (_, _type, _subtype)
local new_base = {}
local properties = {
Type = {
get = function ()
return _type
end
},
Subtype = {
get = function ()
return _subtype
end
}
}
setmetatable( new_base, {
__index = function (_, key)
return (properties[key] and properties[key].get()) or Base[key]
end,
__newindex = function (_, key, value)
if properties[key] and properties[key].set then
properties[key].set( value )
end
end,
} )
return new_base
end
setmetatable( Base, {
__call = new,
} )
return Base
然后我认为一个子类从基类创建如下:
local Base = require( "vyzor.base")
local MiniConsole = Base(“ Component”,“ MiniConsole”)
local function new(_,name,init_x,init_y,init_width,init_height,word_wrap,font_size)
local new_console = {}
<其他本地变量>
local function updateAbsolutes()
<完成任务>
end
local properties = {
Name = {
get = function ()
return name
end
},
Container = {
get = function ()
return container
end,
set = function(value)
if value.Type ==“ Frame” then
container = value
end
end,
<等等>
end
function new_console:Echo(text)
echo( name, text)
end
<其他功能>
setmetatable( new_console,{
__index = function(_,key)
return(properties[key] and properties[key].get())or MiniConsole[key]
end,
__newindex = function(_,key,value)
if属性[key] and properties[key] .set then
properties[key].set(value)
end
end,
})
master_list [name] = new_console
return new_console
end
setmetatable( MiniConsole,{
__index = getmetatable(MiniConsole).__index,
__call =新的,
})
return MiniConsole
如何从MiniConsole类创建一个类,例如具有新的函数和变量,并使用Miniconsole的属性表共享一些更多添加:
我认为它应该像这样开始?
local function new(_,name,title,init_x,init_y,init_width,init_height,word_wrap,font_size)
local new_console = MiniConsole(name,init_x,init_y,init_width,init_height,word_wrap,font_size)
我尝试的所有内容最终都会在我尝试在其他地方使用pcall时给我“循环require或其他错误”错误。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
你可以向任何表格添加函数和数据。因此,你可以这样做:
function newDerivedMC() miniConsole = MiniConsole(...) derived = { yourData = 1, yourMethod = function(self, ...) ... end, } setmetatable(derived, miniConsole) return derived end yourMiniConsole = newDerivedMC()如果你访问
yourMiniConsole中不存在的字段(数据或函数),解释器将查看miniConsole中是否存在。如果你设置一个不存在的字段,同样也是这样。