我很难在Lua学习课程.毫无结果的谷歌搜索让我了解了关于元表的想法,并暗示第三方库是模拟/编写类所必需的.
这是一个示例(仅仅因为我注意到在提供示例代码时我得到了更好的答案):
public class ElectronicDevice { protected bool _isOn; public bool IsOn { get { return _isOn; } set { _isOn = value; } } public void Reboot(){_isOn = false; ResetHardware();_isOn = true; } } public class Router : ElectronicDevice { } public class Modem :ElectronicDevice { public void WarDialNeighborhood(string areaCode) { ElectronicDevice cisco = new Router(); cisco.Reboot(); Reboot(); if (_isOn) StartDialing(areaCode); } }
这是我第一次尝试使用Javier建议的技术来翻译上述内容.
我接受了RBerteig的建议.但是,对派生类的调用仍然会产生:"attempt to call method 'methodName' (a nil value)"
--Everything is a table ElectronicDevice = {}; --Magic happens mt = {__index=ElectronicDevice}; --This must be a constructor function ElectronicDeviceFactory () -- Seems that the metatable holds the fields return setmetatable ({isOn=true}, mt) end -- Simulate properties with get/set functions function ElectronicDevice:getIsOn() return self.isOn end function ElectronicDevice:setIsOn(value) self.isOn = value end function ElectronicDevice:Reboot() self.isOn = false; self:ResetHardware(); self.isOn = true; end function ElectronicDevice:ResetHardware() print('resetting hardware...') end Router = {}; mt_for_router = {__index=Router} --Router inherits from ElectronicDevice Router = setmetatable({},{__index=ElectronicDevice}); --Constructor for subclass, not sure if metatable is supposed to be different function RouterFactory () return setmetatable ({},mt_for_router) end Modem ={}; mt_for_modem = {__index=Modem} --Modem inherits from ElectronicDevice Modem = setmetatable({},{__index=ElectronicDevice}); --Constructor for subclass, not sure if metatable is supposed to be different function ModemFactory () return setmetatable ({},mt_for_modem) end function Modem:WarDialNeighborhood(areaCode) cisco = RouterFactory(); --polymorphism cisco.Reboot(); --Call reboot on a router self.Reboot(); --Call reboot on a modem if (self.isOn) then self:StartDialing(areaCode) end; end function Modem:StartDialing(areaCode) print('now dialing all numbers in ' .. areaCode); end testDevice = ElectronicDeviceFactory(); print("The device is on? " .. (testDevice:getIsOn() and "yes" or "no") ); testDevice:Reboot(); --Ok testRouter = RouterFactory(); testRouter:ResetHardware(); -- nil value testModem = ModemFactory(); testModem:StartDialing('123'); -- nil value
John Calsbee.. 8
这是代码的示例文字转录,有一个有用的Class
库,可以移动到另一个文件.
这绝不是规范的实施Class
; 随意定义您喜欢的对象模型.
Class = {} function Class:new(super) local class, metatable, properties = {}, {}, {} class.metatable = metatable class.properties = properties function metatable:__index(key) local prop = properties[key] if prop then return prop.get(self) elseif class[key] ~= nil then return class[key] elseif super then return super.metatable.__index(self, key) else return nil end end function metatable:__newindex(key, value) local prop = properties[key] if prop then return prop.set(self, value) elseif super then return super.metatable.__newindex(self, key, value) else rawset(self, key, value) end end function class:new(...) local obj = setmetatable({}, self.metatable) if obj.__new then obj:__new(...) end return obj end return class end ElectronicDevice = Class:new() function ElectronicDevice:__new() self.isOn = false end ElectronicDevice.properties.isOn = {} function ElectronicDevice.properties.isOn:get() return self._isOn end function ElectronicDevice.properties.isOn:set(value) self._isOn = value end function ElectronicDevice:Reboot() self._isOn = false self:ResetHardware() self._isOn = true end Router = Class:new(ElectronicDevice) Modem = Class:new(ElectronicDevice) function Modem:WarDialNeighborhood(areaCode) local cisco = Router:new() cisco:Reboot() self:Reboot() if self._isOn then self:StartDialing(areaCode) end end
如果您坚持使用属性的get/set方法,则不需要__index
和__newindex
函数,并且可以只有一个__index
表.在这种情况下,模拟继承的最简单方法是这样的:
BaseClass = {} BaseClass.index = {} BaseClass.metatable = {__index = BaseClass.index} DerivedClass = {} DerivedClass.index = setmetatable({}, {__index = BaseClass.index}) DerivedClass.metatable = {__index = DerivedClass.index}
换句话说,派生类的__index
表"继承"基类的__index
表.这是有效的,因为Lua在委托__index
表时,会有效地重复查找,因此__index
调用表的元方法.
此外,要警惕有关调用obj.Method(...)
VS obj:Method(...)
.obj:Method(...)
是语法糖obj.Method(obj, ...)
,混合两个调用可以产生不寻常的错误.
这是代码的示例文字转录,有一个有用的Class
库,可以移动到另一个文件.
这绝不是规范的实施Class
; 随意定义您喜欢的对象模型.
Class = {} function Class:new(super) local class, metatable, properties = {}, {}, {} class.metatable = metatable class.properties = properties function metatable:__index(key) local prop = properties[key] if prop then return prop.get(self) elseif class[key] ~= nil then return class[key] elseif super then return super.metatable.__index(self, key) else return nil end end function metatable:__newindex(key, value) local prop = properties[key] if prop then return prop.set(self, value) elseif super then return super.metatable.__newindex(self, key, value) else rawset(self, key, value) end end function class:new(...) local obj = setmetatable({}, self.metatable) if obj.__new then obj:__new(...) end return obj end return class end ElectronicDevice = Class:new() function ElectronicDevice:__new() self.isOn = false end ElectronicDevice.properties.isOn = {} function ElectronicDevice.properties.isOn:get() return self._isOn end function ElectronicDevice.properties.isOn:set(value) self._isOn = value end function ElectronicDevice:Reboot() self._isOn = false self:ResetHardware() self._isOn = true end Router = Class:new(ElectronicDevice) Modem = Class:new(ElectronicDevice) function Modem:WarDialNeighborhood(areaCode) local cisco = Router:new() cisco:Reboot() self:Reboot() if self._isOn then self:StartDialing(areaCode) end end
如果您坚持使用属性的get/set方法,则不需要__index
和__newindex
函数,并且可以只有一个__index
表.在这种情况下,模拟继承的最简单方法是这样的:
BaseClass = {} BaseClass.index = {} BaseClass.metatable = {__index = BaseClass.index} DerivedClass = {} DerivedClass.index = setmetatable({}, {__index = BaseClass.index}) DerivedClass.metatable = {__index = DerivedClass.index}
换句话说,派生类的__index
表"继承"基类的__index
表.这是有效的,因为Lua在委托__index
表时,会有效地重复查找,因此__index
调用表的元方法.
此外,要警惕有关调用obj.Method(...)
VS obj:Method(...)
.obj:Method(...)
是语法糖obj.Method(obj, ...)
,混合两个调用可以产生不寻常的错误.
有很多方法可以做到这一点,但这就是我做的事情(通过继承更新):
function newRGB(r, g, b) local rgb={ red = r; green = g; blue = b; setRed = function(self, r) self.red = r; end; setGreen = function(self, g) self.green= g; end; setBlue = function(self, b) self.blue= b; end; show = function(self) print("red=",self.red," blue=",self.blue," green=",self.green); end; } return rgb; end purple = newRGB(128, 0, 128); purple:show(); purple:setRed(180); purple:show(); ---// Does this count as inheritance? function newNamedRGB(name, r, g, b) local nrgb = newRGB(r, g, b); nrgb.__index = nrgb; ---// who is self? nrgb.setName = function(self, n) self.name = n; end; nrgb.show = function(self) print(name,": red=",self.red," blue=",self.blue," green=",self.green); end; return nrgb; end orange = newNamedRGB("orange", 180, 180, 0); orange:show(); orange:setGreen(128); orange:show();
虽然有可能,但我没有实现私有,受保护等.