原文地址http://www.freecls.com/a/2712/1b
lua中跟对象和类最像的就是表格有方法和属性,所以类和对象都只能用表格来模拟
声明一下,其实表格的功能已经能应付大部分场景了,而且lua脚本语言本就不是面向对象语言(它的优势是轻量级简单快速),硬要模拟面向对象有时候会搞得不伦不类,所以以下只为学习,不建议应用到正式环境。
例子
--声明一个基类Web
local Web = {name='freecls', url='http://www.freecls.com', dt='2018', server='aliyun'}
function Web:say_name()
print(self.name)
end
function Web:say_url()
print(self.url)
end
function Web:new(name,url)
local o = {}
setmetatable(o,self)
--将o的元表的__index元方法设置成Web
--那么o中没有的属性就会去Web表格中寻找
self.__index = self
--如果传递了name,url就设置到表格o里
--如果没有就沿用Web里面的内容
if name ~= nil then o.name = name end
if url ~= nil then o.url = url end
return o
end
local w1 = Web:new('沧浪水')
--沧浪水 http://www.freecls.com 2018 aliyun
print(w1.name, w1.url, w1.dt, w1.server)
w1:say_name() --沧浪水
local w2 = Web:new('戴磊', 'http://www.freecls.com/u/info/2712')
--戴磊 http://www.freecls.com/u/info/2712 2018 aliyun
print(w2.name, w2.url, w2.dt, w2.server)
w2:say_name() --戴磊
模拟继承
--声明一个基类Web
--注意这个Web表格为了能被继承改动了很多
local Web = {name='freecls', url='http://www.freecls.com', dt='2018', server='aliyun'}
function Web:say_name()
print(self.name)
end
function Web:say_url()
print(self.url)
end
function Web:new(name,url)
local o = {}
setmetatable(o,self)
--将o的元表的__index元方法设置成Web
--那么o中没有的属性就会去Web表格中寻找
self.__index = self
if name ~= nil then o.name = name else o.name = self.name end
if url ~= nil then o.url = url else o.url = self.url end
o.dt = self.dt
o.server = self.server
o.say_name = self.say_name
o.say_url = self.say_url
return o
end
local Web1 = {age=22,sex='男'}
function Web1:how_old()
print(self.age)
end
--模拟继承
function Web1:new(age,sex)
local o = Web:new()
setmetatable(o, self)
self.__index = self
return o
end
local w1 = Web1:new()
w1.name = '沧浪水'
--22 男 沧浪水 http://www.freecls.com 2018 aliyun
print(w1.age, w1.sex, w1.name, w1.url, w1.dt, w1.server)
w1:say_url() --http://www.freecls.com
w1:say_name() --沧浪水
w1:how_old() --22
模拟重写
local Web = {name='freecls', url='http://www.freecls.com', dt='2018', server='aliyun'}
function Web:say_name()
print(self.name)
end
function Web:say_url()
print(self.url)
end
function Web:new(name,url)
local o = {}
setmetatable(o,self)
--将o的元表的__index元方法设置成Web
--那么o中没有的属性就会去Web表格中寻找
self.__index = self
if name ~= nil then o.name = name else o.name = self.name end
if url ~= nil then o.url = url else o.url = self.url end
o.dt = self.dt
o.server = self.server
o.say_name = self.say_name
o.say_url = self.say_url
return o
end
local Web1 = {age=22,sex='男'}
function Web1:how_old()
print(self.age)
end
--这个方法会失效,重写失败
function Web1:say_name()
print(self.name..' 重写')
end
--模拟继承
function Web1:new(age,sex)
local o = Web:new()
setmetatable(o, self)
self.__index = self
--重写成功
function o:say_name()
print(self.name..' 重写')
end
return o
end
local w1 = Web1:new()
w1.name = '沧浪水'
--22 男 沧浪水 http://www.freecls.com 2018 aliyun
print(w1.age, w1.sex, w1.name, w1.url, w1.dt, w1.server)
w1:say_url() --http://www.freecls.com
w1:say_name() --沧浪水 重写
w1:how_old() --22
总结
1.本文只是对lua模拟面向对象做简单的介绍,如果有疑问可以给我留言
2.lua的版本为5.1,运行环境centos7 64位
3.原文地址http://www.freecls.com/a/2712/1b