Lua屏蔽对象方法和恢复的方法
背景
对于OO思想实现的类, 对于某些场景需要屏蔽某些方法, 不让调用。过了这段场景, 就恢复这些类的方法, 可以调用。
例如:
工厂具有开工方法, 但是在晚上不允许开工, 所有在晚上这段时间, 见开工方法屏蔽掉, 到第二天早上八点将此方法恢复。
实现
local tab = {
new = function()
print("new is called.")
end,
delete = function()
print("delete is called.")
end,
} tab.new()
tab.delete() function setHiddenProperty(tab, name)
if not tab[name] then
return
end if not tab.__hiddenProps then
tab.__hiddenProps = {}
end local hiddenProps = tab.__hiddenProps hiddenProps[name] = tab[name]
tab[name] = nil
end function restoreHiddenPorpertys(tab)
local hiddenProps = tab.__hiddenProps if not hiddenProps then
return
end for k,v in pairs(hiddenProps) do
tab[k] = v
end tab.__hiddenProps = nil
end setHiddenProperty(tab, "new")
setHiddenProperty(tab, "delete") -- this time call new and delete will throw error
--tab.new()
--tab.delete() restoreHiddenPorpertys(tab) tab.new()
tab.delete()
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
new is called.
delete is called.
new is called.
delete is called.
>Exit code: 0
Lua屏蔽对象方法和恢复的方法的更多相关文章
- lua metatable和metamethod元表和元方法
Lua中提供的元表是用于帮助Lua数据变量完成某些非预定义功能的个性化行为,如两个table的相加.假设a和b都是table,通过元表可以定义如何计算表达式a+b.当Lua试图将两个table相加时, ...
- Lua中的元表(metatable)、元方法(metamethod)详解
在第一次看见这两样东西的时候,可能会觉得它很深奥,但其实很好理解,虽然实际上它可能真的很深奥.(小若:停!滚粗.) 1.知道为什么1 + 1 = 2吗? 为什么在Lua中,1+1会等于2呢?(小若:难 ...
- JSon 对象转字符的一些方法
引用System.Web.Entity.dll public static string ToJSON(this object obj) { JavaScriptSerializer serializ ...
- XE3随笔6:SuperObject 的 JSON 对象中还可以包含 "方法"
SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...
- Jquery中使用setInterval和setTimeout会提示缺少对象的错误,解决方法如下:
直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...
- SQL Server中查询用户的对象权限和角色的方法
--SQL Server中查询用户的对象权限和角色的方法 -- 查询用户的object权限 exec sp_helprotect NULL, 'sa' -- 查询用户拥有的role exec sp_h ...
- C++11用于计算函数对象返回类型的统一方法
[C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...
- Android反射出一个类中的其他类对象并调用其对应方法
MainActivity如下: package cn.testreflect; import java.lang.reflect.Field; import java.lang.reflect.Met ...
- java 对象的this使用 java方法中参数传递特性 方法的递归
一.this关键字,使用的情形,以及如何使用. 1.使用的情形 类中的方法体中使用this --初始化该对象 类的构造器中使用this --引用,调用该方法的对象 2.不写this,调用 只要方法或 ...
随机推荐
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 去掉移动端页面 input, textarea, button, a 标签获取焦点时显示的黑影
input, textarea, button, a{ -webkit-tap-highlight-color:rgba(0,0,0,0); }
- ByteBuf和相关辅助类
当我们进行数据传输的时候,往往需要使用到缓冲区,常用的缓冲区就是JDK NIO类库提供的java.nio.Buffer. 实际上,7种基础类型(Boolean除外)都有自己的缓冲区实现,对于NIO编程 ...
- Listener refused the connection with the following error 错误解决
原文地址 :http://blog.csdn.net/zajin/article/details/17753351 做个备份: 查询数据库当前进程的连接数: select count(*) from ...
- Node.js-部署【1】-防火墙端口的配置
原来以为,Node.js部署以后,要手动配置防火墙端口,结果不需要,外网可以访问,看来是自动配好了,真是考虑周到,给我一个大大的惊喜.
- ZeroMQ接口函数之 :zmq_ipc – ZMQ本地进程间通信传输协议
ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html ——————————————————————————————————— ...
- STM32之EXTI——外部中断
互联网的广大网友,大家早上中午晚上好.EXTI...故名思义..EX表外,出..I表示Intrrupt..所以合起来就是外部中断...说到这..我觉得我最近的六级水平(背单词)又进了一步,稍微自夸了下 ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
- linux服务器使用
1.在widows系统下,下载putty.exe 配置默认的服务器IP + 端口 添加名称.点击save即可 参考:http://jingyan.baidu.com/article/c74d60004 ...
- Unity学习疑问记录之查找
unity中提供了获取对象的五种方法: 通过对象名称(Find方法) 通过标签获取单个游戏对象(FindWithTag方法) 通过标签获取多个游戏对象(FindGameObjectsWithTags方 ...