Lua实现窗口类
窗口类需要具备如下特性:
1) 有自己的名字
2) 有自己的布局文件路径
3) 初始化、析构、显示、隐藏
4) 可以拥有一个父窗口,多个子窗口
5) 需要方便通过名字查找其子窗口
6) 具备添加子窗口
7) 脱离父窗口的方法
8) 递归查找指定名字的子窗口
9) 递归删除指定名字的子窗口
10) 控制台打印出某个窗口的整个父子关系
Base_Windows = {}
function Base_Windows:Create()local windows = {name,layoutPath,parent,children}--初始化function windows:Init(name)self.name = nameself.layoutPath = ''self.parent = {}self.children = {}end--添加子窗口function windows:AddWindows(name)local child = Base_Windows:Create()child.name = namechild.parent = selfchild.children = {}table.insert(self.children,child)return childend--脱离父窗口function windows:Leave()local parent = self.parentfor key,child in pairs(parent.children) doif child.name == self.name thenparent.children[key] = nilendendself.parent = nilend--查找子窗口function windows:FindChild(name)local children = self.childrenif children~=nil thenfor key,child in pairs(children) doif child.name == name thenprint('success find child :'..child.name..' '..child.parent.name)return childelsechild:FindChild(name)endendendend--删除子窗口function windows:DeleChild(name)local children = self.childrenif children~=nil thenfor key,child in pairs(children) doif child.name == name thenchild:Destory()elsechild:DeleChild(name)endendendend--打印层级关系function windows:PrintRelation()print(self.name)local children = self.childrenif children~=nil thenfor key,child in pairs(children) dochild:PrintRelation()endendend--销毁窗口function windows:Destory()local children = self.childrenif children~=nil thenfor key,child in pairs(children) dochild:Destory()endendself.name = nilself.layoutPath = nilself.parent = nilself.children = nilself = nilend--展示与隐藏,见下题function windows:Display()endfunction windows:Hide()endreturn windows
end调用方法
local windows = Base_Windows:Create()
windows:Init('windows')
local windows1 = windows:AddWindows('windows1')
local windows2 = windows:AddWindows('windows2')
windows1:AddWindows('windows11')
windows2:AddWindows('windows21')
windows:FindChild('windows21')
windows:Display()
windows1:Leave()
windows2:DeleChild('windows21')
windows:Display()
windows:Destory()
本文发布于:2024-02-05 01:42:54,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170721170361871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |