在使用面向对象操作的封闭方法时,如何实现受保护的成员? [英] how do implemented protected members when using the closure approach to OOP?
问题描述
现在,我正在使用在Lua中实现OOP的闭包.下面是一个简短的示例.尝试在infested_mariner
中实现stronger_heal
时发生我的问题.
Right now I am using closures for implementing OOP in Lua. An abridged example follows. My issue happens when trying to implement stronger_heal
inside infested_mariner
.
--------------------
-- 'mariner module':
--------------------
mariner = {}
-- Global private variables:
local idcounter = 0
local defaultmaxhp = 200
local defaultshield = 10
function mariner.new ()
local self = {}
-- Private variables:
local hp = maxhp
-- Public methods:
function self.sethp (newhp)
hp = math.min (maxhp, newhp)
end
function self.gethp ()
return hp
end
function self.setarmorclass (value)
armorclass = value
updatearmor ()
end
return self
end
-----------------------------
-- 'infested_mariner' module:
-----------------------------
-- Polymorphism sample
infested_mariner = {}
function infested_mariner.bless (self)
-- New methods:
function self.strongerheal (value)
-- how to access hp here?
hp = hp + value*2
end
return self
end
function infested_mariner.new ()
return infested_mariner.bless (mariner.new ())
end
如果将我的infested_mariner
定义放在另一个.lua文件中,它将无法访问基本.lua文件中定义的全局私有变量或私有变量.如何保护只有infested_mariner
可以访问的成员,并且解决方案不涉及将所有派生类与父类放在同一个文件中?
If I place my infested_mariner
definition in another .lua file, it won't be able to access the global private variables, or access to the private variables, defined in the base .lua file. How do I have protected members that only infested_mariner
can access, and the solution doesn't involve having all the derived classes in the same file as the parent?
注意:我目前暂时在子类中使用getter和setters.
Note: I am currently using getters and setters in the child class for now.
推荐答案
在Lua中,您只能访问其作用域中的局部变量.为了允许其他函数查看您的变量,您需要对其进行重写,以使受保护的变量位于子类可以访问的表中.
In Lua, you can only access local variables in their scope. In order to allow other functions to see your variables you will need to rewrite it so the protected variables are in a table that is accessible by the child class.
做到这一点的一种方法是仅在当前类中公开公共属性,并使用命名约定(例如以下划线开头的名称)来表示受保护的内容.您可能知道这一点,但是我不得不说,我认为这种方法通常比真正的受保护变量更容易实现.
One way to do this is by just making public properties in the current class and use a naming convention (like names starting with a underscore) to denote the protected stuff. You probably know this but I have to say that I think this approach is usually much simpler to implement than real protected variables.
如果要使用真正的受保护变量,则需要将表分开以供公共和受保护的东西使用.一种方法是更改祝福功能,以使其接收以下两个表:
If you want real protected variables, you need to separate the table for the public and the protected stuff. One approach is to change the bless function so that it receives both these tables:
function infested_mariner.bless (pub, pro)
-- New methods:
function pub.strongerheal (value)
pro.hp = pro.hp + value*2
end
return pub
end
如何进行设置,以使构造函数彼此之间传递受保护的表,这是一个练习.如果您走这条路线,您可能希望为它做一些功能,以免您每天没有机会碰触受保护的桌子.
How to set things up so that constructors pass the protected table to each other is left an exercise. If you go this route you probably want to have some function doing it for you so that you don't get a chance to touch the protected table in a day to day basis.
这篇关于在使用面向对象操作的封闭方法时,如何实现受保护的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!