Lua面向对象之二:类继承
1.类继承
①代码
Sharp = { _val = 1} --① 父类
function Sharp:new()
local new_sharp = { }
self.__index = self --②,self == Sharp
setmetatable(new_sharp, self) --③
return new_sharp
end
function Sharp:sharp_func()
print("Sharp call sharp_func")
end
function Sharp:name()
print("Sharp call name")
end
function Sharp:val()
print(string.format("Sharp call val %d", self._val))
end
Circle = Sharp:new() --① 子类
function Circle:new()
local new_circle = {}
self.__index = self --②,self == Circle
setmetatable(new_circle, self) --③
return new_circle
end
--新函数
function Circle:circle_func()
print("Circle call circle_func")
end
--覆盖函数name
function Circle:name()
print("Circle call name")
end
--覆盖函数val
function Circle:val()
print(string.format("Circle call val %d", self._val))
end
②调用代码
local circle = Circle:new()
circle._val = 2 --覆盖赋值
circle:sharp_func() --调用父类函数
circle:circle_func() --调用新函数
circle:name() --调用覆盖函数
circle:val() --调用覆盖函数
③输出结果
Lua面向对象: