Lua的面向对象程序设计
Account={balance=}
function Account.withdraw(self,v)
self.balance=self.balance-v
end a={balance=,withdraw=Account.withdraw}
a.withdraw(a,) --面向对象语言常使用self参数,lua提供了通过使用冒号操作符来隐藏self参数的声明 function Account:withdraw(v)
self.balance=self.balance-v
end
a:withdraw(100.0)
--冒号的效果相当于在函数定义和函数调用的时候,增加一个额外的隐藏参数
--我们可以使用dot语法定义函数而用冒号语法调用函数,反之亦然,只要我们正确的处理好额外的参数.
--dot语法定义时要加上self参数,调用时要传入相应的对象,冒号语法不用self参数,调用时也不需要相应的参数对象
Lua中对象没有类,每个对象有一个原型prototype,当调用不属于对象的某些操作时,会最先会到prototype中查找这些操作。
Account={balance=} function Account:withdraw(v)
self.balance=self.balance-v
end function Account:deposit(v)
self.balance=self.balance+v
end function Account:new(o)--返回一个子对象
o=o or {}
setmetatable(o,self)--Account成为o的原型
self.__index=self
return o
end a=Account:new{balance=}
print(a.balance)--输出0
a:deposit(100.00)
print(a.balance)--输出100
Account={balance=} function Account:withdraw(v)
if v>self.balance then error"insufficient funds" end
self.balance=self.balance-v
end function Account:deposit(v)
self.balance=self.balance+v
end function Account:new(o)
o=o or {}
setmetatable(o,self)--Account成为o的原型
self.__index=self
return o
end SpecialAccount=Account:new() function SpecialAccount:getLimit()
return self.limit or
end
--子类可以重定义从父类中继承来的方法
function SpecialAccount:withdraw(v)
if v-self.balance>=self:getLimit() then
error"insufficeint funds"
end
self.balance=self.balance-v
end s=SpecialAccount:new{limit=1000.00} function s:getLimit()
return self.balance*0.1
end s:withdraw(200.0)--该调用将运行SpecialAccount的withdraw方法,但是当
--方法调用self:getLimit时,最后的定义被触发.
多重继承:将函数用作__index。当一个表的metatable存在一个__index函数时,如果lua调用一个原始表中不存在的函数,Lua将调用这个__index指定的函数,这样可以用__index实现在多个父类中查找子类不存在的域。
--原型,相当于父类
Account={money=} function Account:save(v)
self.money=self.money+v
end function Account:spend(v)
self.money=self.money-v
if self.money< then
error"there is not enough money"
end
end function Account:new(o)
o=o or {}
setmetatable(o,self)
self.__index=self--首先在o的原型中查找,然后在原型的__index中查找
return o
end
--k是缺少的域,plist是table,其元素也是table
function search(k,plist)
for i=,table.getn(plist) do
local v=plist[i][k]
if v then return v end
end
end function createClass(...)
local c={}
--__index是函数时,Lua将table和其缺少的域作为参数调用这个函数
setmetatable(c,{__index=function(t,k) print(t,k) return search(k,arg) end})
c.__index=c
function c:new(o)
o=o or {}
setmetatable(o,c)
return o
end
return c
end Named={} function Named:getname()
return self.name
end function Named:setname(s)
self.name=s
end NamedAccount=createClass(Account,Named)
account=NamedAccount:new{name="paul"}
print(NamedAccount)
print(account:getname()) --[[
table: 0039C988
table: 0039C988 getname
paul
]]
私有性
function newAccount(initialBalance)
--存储在self表中的部分都是私有的
local self={balance=initialBalance} local withdraw=function(v)
self.balance=self.balance-v
end local deposit=function(v)
self.balance=self.balance+v
end local getBalance=function() return self.balance end return {
withdraw=withdraw,
deposit=deposit,
getBalance=getBalance
}
end instance=newAccount()
print(instance.getBalance())
instance.withdraw()
print(instance.getBalance())
single-method的对象实现方法:当对象只有一个单一的方法时,可以将这个单一的方法作为对象返回
function newObject(value)
return function(action,v)
if action=="get" then return value
elseif action=="set" then value=v
else error("invalid action")
end
end
end
--每一个对象是一个单独的闭包,代价比表小得多,这种方式没有继承但有私有性:访问
--对象状态的唯一状态是通过它的内部方法
d=newObject()--d这个对象是newObject返回的单一方法
print(d("get"))--
d("set",)
print(d("get"))--
Lua的面向对象程序设计的更多相关文章
- [.net 面向对象程序设计深入](0) 开篇
[.net 面向对象程序设计深入](0)开篇 [.net 面向对象编程基础]和 [.net 面向对象程序设计进阶]在15年底写完了,群里也加进来不少热爱学习的小伙伴.让我深切感受到在这个 ...
- [.net 面向对象程序设计进阶] (1) 开篇
[.net 面向对象程序设计进阶] (1) 开篇 上一系列文章<.net 面向对象编程基础>写完后,很多小伙伴们希望我有时间再写一点进阶的文章,于是有了这个系列文章.这一系列的文章中, 对 ...
- [.net 面向对象程序设计深入](6).NET MVC 6 —— 模型、视图、控制器、路由等的基本操作
[.net 面向对象程序设计深入](6).NET MVC 6 —— 模型.视图.控制器.路由等的基本操作 1. 使用Visual Studio 2015创建Web App (1)文件>新建> ...
- [.net 面向对象程序设计深入](5)MVC 6 —— 构建跨平台.NET开发环境(Windows/Mac OS X/Linux)
[.net 面向对象程序设计深入](5)MVC 6 —— 构建跨平台.NET开发环境(Windows/Mac OS X/Linux) 1.关于跨平台 上篇中介绍了MVC的发展历程,说到ASP.NET ...
- [.net 面向对象程序设计深入](4)MVC 6 —— 谈谈MVC的版本变迁及新版本6.0发展方向
[.net 面向对象程序设计深入](4)MVC 6 ——谈谈MVC的版本变迁及新版本6.0发展方向 1.关于MVC 在本篇中不再详细介绍MVC的基础概念,这些东西百度要比我写的全面多了,MVC从1.0 ...
- [.net 面向对象程序设计深入](3)UML——在Visual Studio 2013/2015中设计UML活动图
[.net 面向对象程序设计深入](3)UML——在Visual Studio 2013/2015中设计UML活动图 1.活动图简介 定义:是阐明了业务用例实现的工作流程. 业务工作流程说明了业务为向 ...
- [.net 面向对象程序设计深入](2)UML——在Visual Studio 2013/2015中设计UML用例图
[.net 面向对象程序设计深入](2)UML——在Visual Studio 2013/2015中设计UML用例图 1.用例图简介 定义:用例图主要用来描述“用户.需求.系统功能单元”之间的关系. ...
- [.net 面向对象程序设计深入](1)UML——在Visual Studio 2013/2015中设计UML类图
[.net 面向对象程序设计深入](1)UML——在Visual Studio 2013/2015中设计UML类图 1.UML简介 Unified Modeling Language (UML)又称统 ...
- [.net 面向对象程序设计进阶] (28) 结束语——告别2015
[.net 面向对象程序设计进阶] (28) 结束语——告别2015 <.net面向对象程序设计进阶>这一系列文章写了太长的时间了,大概有半年没写,在年底又一口气写了好几篇.在整个过程中目 ...
随机推荐
- [译]libcurl错误码
CURLcode Almost all "easy" interface functions return a CURLcode error code. No matter wha ...
- ASP.NET文件操作
在开发Web程序时,不但有存储在数据库中和XML文件中的数据形式需要处理,而且还有很多诸如文本.Word文档和图片等格式的文件数据需要处理.尤其是在一些信息管理系统中,文档的处理流程贯穿了整个系统的运 ...
- EasyUI系列学习(七)-Linkbutton(按钮)
一.加载组件 1.使用class加载 <a href="#" class="easyui-linkbutton">按钮</a> 2.使用 ...
- idea安装插件plugin(主要针对网络连接不上的情况)
https://blog.csdn.net/duoduo1636546/article/details/80104711
- LN : leetcode 515 Find Largest Value in Each Tree Row
lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...
- Java8(一)--lambda表达式
相信作为一个Java程序员都会或多或少的了解过Java8中的lambda表达式.函数式编程等,本人也是用过lambda表达式,使用的都是比较简单 的实现 通过一个例子去都感受lambda: Compa ...
- 微服务网关从零搭建——(一)创建测试api以及api自动注入consul
本系列编写目的纯属个人开发记录 以下代码均为demo级 如有需要 请自行优化 代码完整包由于公司电脑加密 无法上传整包的demo文件 consul 开发环境简易处理 consul 下载地址 : ht ...
- git学习(2)----入门
一.git.github和gitlab的区别 Git诞生于2005年,大神Linus的作品,Github诞生于2008年,没有Git就没有GitHub,Github已成为全球最大的代(tong)码(x ...
- xmpp使用经验
IM 标准协议有XMPP\IMPP\PRIM\SIP(SIMPLE)等,其中XMPP基于XML的协议,具备了很好的扩展性(依靠XML的域名空间)并且可以建立在TLS上使用SASL认证. 1.文件比如图 ...
- xmpp 消息和好友上下线(3)
原始地址:XMPPFrameWork IOS 开发(四) 消息 //收到消息 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XM ...