16 – Object-Oriented Programming
2016-09-05 18:22
316 查看
16 – Object-Oriented Programming
A table in Lua is an object in more than one sense. Like objects, tables have a state. Like objects, tables have an identity (a selfness) that is independent of their values; specifically, two objects (tables) with the same value are different objects,whereas an object can have different values at different times, but it is always the same object. Like objects, tables have a life cycle that is independent of who created them or where they were created.
Objects have their own operations. Tables also can have operations:
Account = {balance = 0} function Account.withdraw (v) Account.balance = Account.balance - v end
This definition creates a new function and
stores it in field
withdrawof
the
Accountobject.
Then, we can call it as
Account.withdraw(100.00)
This kind of function is almost what we call a method. However, the use of the global name
Accountinside the function is
a bad programming practice. First, this function will work only for this particular object. Second, even for this particular object the function will
work only as long as the object is stored in that particular global variable; if we change the name of this object,
withdrawdoes not work any more:
a = Account; Account = nil a.withdraw(100.00) -- ERROR!
Such behavior violates the previous principle that objects have independent
life cycles.
A more flexible approach is to operate on the receiver of the operation. For that, we would have to define our method with an extra parameter, which tells the method on which object it has to operate. This parameter usually has the name self or this:
function Account.withdraw (self, v) self.balance = self.balance - v end
Now, when we call the method we have to specify on which object it has to
operate:
a1 = Account; Account = nil ... a1.withdraw(a1, 100.00) -- OK
With the use of a self parameter,
we can use the same method for many objects:
a2 = {balance=0, withdraw = Account.withdraw} ... a2.withdraw(a2, 260.00)
This use of a self parameter is a central point in any object-oriented language. Most OO languages have this mechanism partly hidden from the programmer, so that she does not have to declare this parameter (although she still can use the name self or this inside
a method). Lua can also hide this parameter, using the colon operator. We can rewrite the previous method definition as
function Account:withdraw (v) self.balance = self.balance - v end
and the method call as
a:withdraw(100.00)
The effect of the colon is to add an extra hidden parameter in a method definition
and to add an extra argument in a method call. The colon is only a syntactic facility, although a convenient one; there is nothing really new here. We can define a function with
the dot syntax and call it with the colon syntax, or vice-versa, as long as we handle the extra parameter correctly:
Account = { balance=0, withdraw = function (self, v) self.balance = self.balance - v end } function Account:deposit (v) self.balance = self.balance + v end Account.deposit(Account, 200.00) Account:withdraw(100.00)
Now our objects have an identity, a state, and operations over this state. They still lack a class system, inheritance, and privacy. Let us tackle the first problem: How can we create several objects with similar behavior? Specifically, how can we create several
accounts?
相关文章推荐
- JAVA提高篇(16)--ObjectInputStream和ObjectOutputStream类
- Java基础系列16:使用JSONObject和JSONArray解析和构造json字符串
- error while loading shared libraries: libmysqlclient.so.16/18: cannot open shared object file
- error while loading shared libraries: libmysqlclient.so.16/18: cannot open shared object file
- R-FCN: Object Detection via Region-based Fully Convolutional Networks (NIPS 16), Arxiv 16.05
- fedora 16 x64 安装gnustep object-c开发环境
- dlib 16 dlib自带demo Max-Margin Object Detection(MMOD)
- Object-c 纯手写方法CRC8,16,32,跟我用笔计算的一样,不过跟那些代码算法结果32位的不同
- IOS开发笔记16-Object-C中的属性
- ogre3D学习基础16 -- 手动创建实体(ManualObject)
- JSON 之 SuperObject(16): 实例 - 解析 Google 关键字搜索排名
- object-c将RGB颜色转换成16进制HTML颜色
- chapter 16 Object-Oriented Programming
- 《Inside the C++ Object Model》笔记(1~7章)
- c#中int 转string 16进制和16转double的方法
- tcpip详解笔记(16) TCP的交互数据流
- 16--2--2指针变量交换
- 黑马程序员11--Object-C--07--Block
- Object-C 语法总结
- Object 详解