网站存在的缺陷,云南软件开发公司,网络设计参考文献,小程序开发费用多少Lua中的面向对象是通过表#xff08;table#xff09;来模拟类实现的#xff0c;通过setmetatable(table,metatable)方法#xff0c;将一个表设置为当前表的元表#xff0c;之后在调用当前表没有的方法或者键时#xff0c;会再查询元表中的方法和键#xff0c;以此来实现…Lua中的面向对象是通过表table来模拟类实现的通过setmetatable(table,metatable)方法将一个表设置为当前表的元表之后在调用当前表没有的方法或者键时会再查询元表中的方法和键以此来实现面向对象。
至于元表和元方法的使用可以看我的这篇文章
Lua元表和元方法的使用-CSDN博客
一个例子来说明实现
有一家三口爸爸是工程师妈妈是老师孩子是学生都会跑步但是他们从事不同的工作。
实现封装
local people {}function people:new ()local t {}setmetatable(t,self);self.__index self;return t
endfunction people:talk()print(Im a person)
endfunction people:running()print(I can run)
end
实现继承
local engineer people:new();local teacher people:new();local student people:new();engineer.running();teacher.running();student.running(); 输出 实现多态
local engineer people:new();function engineer:talk()print(Im an engineer)
endlocal teacher people:new();function teacher:talk()print(I am a teacher)
endlocal student people:new();function student:talk()print(I am a student)
endengineer.talk();teacher.talk();student.talk();
输出 参考书籍与链接
《Lua程序设计》
《Cocos2d-x游戏开发手把手教你Lua语言的编程方法》
掌握 Lua 脚本语言 (pikuma.com)
Creating A Toggle Switch In Wix Studio (youtube.com)