1.子类重写父类函数的方法
在 Lua 中实现面向对象编程时,确实可以让子类重写父类函数的同时复用父类的逻辑。以下是实现这种模式的几种方法:
1.1 基础实现方式
-- 父类local Parent = {}
Parent.__index = Parent
function Parent:new()
local obj = {}
setmetatable(obj, self)
return objend
function Parent:start()
print("父类start: 执行通用初始化操作")
-- 通用参数设置等
self.commonProperty = "所有子类共享的属性"end
-- 子类local Child = Parent:new() -- 继承父类
Child.__index = Child
function Child:start()
-- 先调用父类的start方法
Parent.start(self) -- 然后执行子类特有的逻辑
print("子类start: 执行特有操作")
self.specialProperty = "子类特有的属性"end
-- 使用示例local child = Child:new()
child:start()-- 输出:-- 父类start: 执行通用初始化操作-- 子类start: 执行特有操作
1.2 使用 super 关键字模式(更优雅的实现)
-- 创建类系统的基础设施local function class()
local cls = {}
cls.__index = cls function cls:new(…)
local obj = setmetatable({}, self)
if obj.ctor then
obj:ctor(…)
end
return obj
end -- 添加继承功能
function cls:extend()
local subclass = {}
subclass.super = self -- 保存父类引用
setmetatable(subclass, {__index = self})
subclass.__index = subclass
return subclass
end return clsend
-- 父类定义local Parent = class()
function Parent:ctor()
-- 构造函数end
function Parent:start()
print("父类start: 执行通用初始化")
self.commonValue = 100end
-- 子类定义local Child = Parent:extend()
function Child:start()
-- 调用父类方法
self.super.start(self) -- 子类特有逻辑
print("子类start: 执行特有逻辑")
self.childValue = self.commonValue + 50 -- 可以访问父类设置的属性end
-- 使用示例local obj = Child:new()
obj:start()-- 输出:-- 父类start: 执行通用初始化-- 子类start: 执行特有逻辑print(obj.commonValue, obj.childValue) -- 输出: 100 150
1.3 使用闭包保存父类方法(更灵活的方式)
-- 父类local Parent = {}
function Parent:new()
local obj = {}
setmetatable(obj, {__index = self}) -- 保存原始start方法
obj.parentStart = self.start return objend
function Parent:start()
print("父类start: 通用操作")
self.baseConfig = {width = 100, height = 200}end
-- 子类local Child = Parent:new()
function Child:start()
-- 调用父类方法
self:parentStart() -- 子类特有逻辑
print("子类start: 扩展操作")
self.childConfig = {color = "red"}end
-- 使用示例local instance = Child:new()
instance:start()-- 输出:-- 父类start: 通用操作-- 子类start: 扩展操作
1.4 使用元表链式调用(高级技巧
local function inherit(parent)
local child = {}
setmetatable(child, {__index = parent}) -- 重写方法时自动调用父类方法
function child:override(methodName, newMethod)
local parentMethod = parent[methodName] self[methodName] = function(...) if parentMethod then parentMethod(...) end return newMethod(...) end
end return childend
-- 父类local Parent = {}function Parent:start()
print("父类start: 基础配置")end
-- 子类local Child = inherit(Parent)
Child:override("start", function(self)
print("子类start: 扩展配置")end)
-- 使用示例local obj = setmetatable({}, {__index = Child})
obj:start()-- 输出:-- 父类start: 基础配置-- 子类start: 扩展配置
2. 多继承实现
多继承场景下的正确模式
#若需要实现多继承,应通过 混合元表 实现,但仍需保持构造函数的独立性。以下是一个安全的多继承构造函数模板:
lua
#-- 基类 A
local A = {}
A.__index = A
function A.new()
local obj = {}
setmetatable(obj, A)
return obj
end
#-- 基类 B
local B = {}
B.__index = B
function B.new()
local obj = {}
setmetatable(obj, B)
return obj
end
#-- 子类 C 继承 A 和 B
local C = {}
C.__index = C
#-- 多继承构造函数
function C.new()
local obj = {}
-- 合并基类方法(根据优先级)
local meta = {
__index = function(t, k)
return A[k] or B[k] -- 按优先级查找
end
}
setmetatable(obj, meta)
-- 初始化子类特有属性
obj._specs = {}
return obj
end
发表回复