Object-Oriented Programming

Lua has only one data-structure mechanism, the table. Tables are first-class,
dynamically created associative arrays.
Table是Lua语言仅仅有一个数据结构机制。
Table作为first-class 动态创建的关联数组.

Tables plus first-class functions already give Lua partial support for objects.
An object may be represented by a table: instance variables are regular table
fields and methods are table fields containing functions.
In particular, tables have identity.
That is, a table is different from other tables even if they have the
same contents, but it is equal to itself even if it changes its contents over time.
Table和first-class函数提供了部分支持对象的机制。
一个Table就代表了一个对象Object:成员变量就是Table的字段,方法就是Table中包含的first-class方法。
特别地,Table具有唯一性。
尽管两个Table有一样的内容,但是Table仅仅和自己相等。

One missing ingredient in the mix of tables with first-class functions is how
to connect method calls with their respective objects.

使用Table和first-class函数的缺失部分是如何表示对象的成员函数。

If obj is a table with a method foo and we call obj.foo(), foo will have no reference to obj.

如果obj是一个带有foo函数的table,当调用obj.foo()函数时,foo没有引用obj.
We could solve this problem by making foo a closure with an internal reference to obj,
but that is expensive, as each object would need its own closure for each of its
methods.

A better mechanism would be to pass the receiver as a hidden argument to
the method, as most object-oriented languages do.
Lua提供一个更好的机制来实现像面向对象语言那样来隐藏函数的参数到调用者那里。
Lua supports this mechanism with a dedicated syntactic sugar, the colon operator: the syntax orb:foo() is
sugar for orb.foo(orb), so that the receiver is passed as an extra argument to the method.
There is a similar sugar for method definitions.

Lua提供一个语法糖机制,使用冒号符号来隐藏函数的参数。
 
The syntax
function obj:foo (...) ... end

is sugar for
obj.foo = function (self, ...) ... end

That is, the colon adds an extra parameter to the function, with the fixed name self.
The function body then may access instance variables as regular fields of table self.

To implement classes and inheritance, Lua uses delegation.
Delegation in Lua is very simple and is not directly connected with object-oriented programming;
it is a concept that applies to any table.Any table may have a designated “parent” table.
为了在Lua语言中实现类和继承,Lua使用delegation来实现。
在Lua里面的delegation 是非常简单,并且不直接管理面向对象语言。
delegation概念被应用到任何Table。Table可以拥有一个设计好的"父类"Table.
Whenever Lua fails to find a field in a table, it tries to find that field in the parent table.
In other words, Lua delegates field accesses instead of method calls.
当Lua在Table中查询到字段失败时,Lua就尝试在父类Table中查询字段。
换言之,Lua delegation field 用来代替函数调用。

Let us see how this works. Let us assume an object obj and a call obj:foo().
This call actually means obj.foo(obj), so Lua first looks for the key foo in table obj.
If obj has such field, the call proceeds normally.
Otherwise, Lua looks for that key in the parent of obj.

Once it found a value for that key, Lua calls the value (which should be a function) with the original object obj as the first argument, so that obj becomes the value of the parameter self inside the method’s body.

With delegation, a class is simply an object that keeps methods to be used by its instances.
A class object typically has constructor methods too, which are used by the class itself.

通过delegation,一个类型Class是一个简单的拥有方法的对象。
一个类对象同时拥有构造方法。
A constructor method creates a new table and makes it delegates its accesses to the class, so that any class method works over the new object.
If the parent object has a parent, the query for a method may trigger another query in the parent’s parent, and so on.
一个构造方法创建一个新Table,同时,Table的代理对应着类型.所以,新对象拥有类的成员方法。
Therefore, we may use the same delegation mechanism to implement inheritance.
所以,在Lua语言中使用代理机制来实现类的继承。
In this setting, an object representing a (sub)class delegates accesses to unknown methods to another object  representing its superclass.
For more advanced uses, a program may set a function as the parent of a table.
In that case, whenever Lua cannot find a key in the table it calls the parent function to do the query.
 
This mechanism allows several useful patterns, such as multiple inheritance and inter-language inheritance (where a Lua object may delegate to a C object, for instance).
代理机制允许多种有用的设计模式,例如多重继承和过渡语言继承(使用Lua对象可以代理在C语言对象)

Programming with Multiple Paradigms in Lua(Object-Oriented Programming)相关推荐

  1. Java OOP(Object Oriented Programming)个人理解及总结

    面向对象编程(Object Oriented Programming,OOP,面向对象程序设计) 其三大特征:封装,继承,多态: 封装:解决数据的安全问题. 继承:解决代码的重用问题. 多态:解决程序 ...

  2. 面向对象编程(Object Oriented Programming)概念总结及延伸(一)

    1.介绍 笔者的梦想是成为一个架构师,但是要成为一个合格的架构师是相当不易的,它既需要丰富的项目经验也需要不断地吸取新的知识,而且在这过程中我们也要不断巩固基础知识.我也注意到了,现在主流的文章大都集 ...

  3. 面向对象数据库(Object Oriented Databases)

    前面说几句费话.现在正在从事面向对象数据库在国内的推广工作,如果有兴趣可以与我联系.如果有任何问题可以私信我,也可以到我们网站上 面向对象数据库交流社区 来向我提问,我一定以最快的速度解答. 想把 面 ...

  4. 夯实基础,彻底掌握js的核心技术(二):面向对象编程(Object Oriented Programming)

    单例设计模式(Singleton Pattern) 1. 单例模式解决的问题: 表现形式: Var obj = {xxx; xxx, - } 2. 作用: 把描述同一件事物的属性和特征进行" ...

  5. python三大特征六大原则_面向对象程序设计(Object Oriented Programming)的三大特性,六大原则...

    三大特性 封装.继承.多态性 拿简单工厂模式举例: namespace DesignMode_01 { // 计算基类 public class Operation { private double ...

  6. Object Oriented Programming面向对象编程

    OOP 面向对象编程( Object Oriented Programming)是一种 计算机编程 架构.OOP 的一条基本原则是 计算机程序是由单个能够起到子 程序作用的单元或 对象组合而成.OOP ...

  7. Java SE 008 理解面向对象程序设计 (Inside Object Oriented Programming)

    Java SE 008 理解面向对象程序设计 (Inside Object Oriented Programming) 前言:此笔记为圣思园张龙老师讲述的java视频课程笔记,自己看视频学习时记录的, ...

  8. python Object Oriented Programming

    python 知识点整理(五) 本文只是对python部分知识点进行学习和整理 本篇主要是针对python的Object Oriented Programming的总结 本文目录 python 知识点 ...

  9. Python编程基础:第三十九节 面向对象编程Object Oriented Programming

    第三十九节 面向对象编程Object Oriented Programming 前言 实践 前言 到目前为止我们都是函数式编程,也即将每一个功能块写为一个函数.其实还有一种更常用的编程方式被称为面向对 ...

最新文章

  1. SAP MM初阶事务代码MEK1维护PB00价格
  2. NET中验证控件表达式汇总
  3. Andriod anim alpha中的属性介绍
  4. java中获取时间6,Java中获取指定日为星期几及其他日期操作(2)
  5. java 比较算法_JAVA排序算法实现和比较:冒泡,桶,选择,快排,归并
  6. leetcode 95. Unique Binary Search Trees II | 96. Unique Binary Search Trees
  7. vue --- 使用vue在html上显示当前时间
  8. Appium基础四:Desired Capabilities详讲
  9. 生产用计算机房噪音应小于,机房建设规范标准要求
  10. Android移动客户端性能测试浅谈——电量
  11. VS的Qt界面预览和QtCreator的界面预览快捷键
  12. 三大组件之Spring 第四章Spring与DAO
  13. Mysql(多级分销)无限极数据库表设计方法
  14. 关于“DEP数据执行保护”的解决方案
  15. 人月神话(12)干将莫邪
  16. Java打造RPC框架(四):支持zookeeper与负载均衡
  17. 用VHDL编写testbench激励文件
  18. Linux的安装(一步一步教你安装Linux)
  19. 【睡服】自动化面试官,就用2020年最全的自动化测试面试题及答案
  20. 如何修改DOSBOX的窗口大小

热门文章

  1. 2005/4.16/多云转晴
  2. 笔记本换键盘详细教程
  3. python仓库 nexus_Python python-nexus包_程序模块 - PyPI - Python中文网
  4. 数据库练习题第二篇(附答案)
  5. 如何让下载并安装wireshark
  6. 我的世界Forge版开服教程
  7. java 用面向接口编程的方式开发打印机_Java面向接口编程之简单工厂模式示例
  8. 【感悟】——人生路,昂首走
  9. MySQL数据库服务器配置
  10. 年终奖发放前离职的员工是否有权获得年终奖