Lua 面向对象


面向对象编程(Object Oriented Programming,OOP)是一种非常流行的计算机编程架构。

以下几种编程语言都支持面向对象编程:

  • C++
  • Java
  • Objective-C
  • Smalltalk
  • C#
  • Ruby

面向对象特征

  • 1) 封装:指能够把一个实体的信息、功能、响应都装入一个单独的对象中的特性。
  • 2) 继承:继承的方法允许在不改动原程序的基础上对其进行扩充,这样使得原功能得以保存,而新功能也得以扩展。这有利于减少重复编码,提高软件的开发效率。
  • 3) 多态:同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果。在运行时,可以通过指向基类的指针,来调用实现派生类中的方法。
  • 4)抽象:抽象(Abstraction)是简化复杂的现实问题的途径,它可以为具体问题找到最恰当的类定义,并且可以在最恰当的继承级别解释问题。

Lua 中面向对象

我们知道,对象由属性和方法组成。LUA中最基本的结构是table,所以需要用table来描述对象的属性。

lua中的function可以用来表示方法。那么LUA中的类可以通过table + function模拟出来。

至于继承,可以通过metetable模拟出来(不推荐用,只模拟最基本的对象大部分时间够用了)。

Lua中的表不仅在某种意义上是一种对象。像对象一样,表也有状态(成员变量);也有与对象的值独立的本性,特别是拥有两个不同值的对象(table)代表两个不同的对象;一个对象在不同的时候也可以有不同的值,但他始终是一个对象;与对象类似,表的生命周期与其由什么创建、在哪创建没有关系。对象有他们的成员函数,表也有:

  1. Account = {balance = 0}
  2. function Account.withdraw (v)
  3. Account.balance = Account.balance - v
  4. end

这个定义创建了一个新的函数,并且保存在Account对象的withdraw域内,下面我们可以这样调用:

  1. Account.withdraw(100.00)

一个简单实例

以下简单的类包含了三个属性: area, length 和 breadth,printArea方法用于打印计算结果:

  1. -- Meta class
  2. Rectangle = {area = 0, length = 0, breadth = 0}
  3.  
  4. -- 派生类的方法 new
  5. function Rectangle:new (o,length,breadth)
  6. o = o or {}
  7. setmetatable(o, self)
  8. self.__index = self
  9. self.length = length or 0
  10. self.breadth = breadth or 0
  11. self.area = length*breadth;
  12. return o
  13. end
  14.  
  15. -- 派生类的方法 printArea
  16. function Rectangle:printArea ()
  17. print("矩形面积为 ",self.area)
  18. end

创建对象

创建对象是位类的实例分配内存的过程。每个类都有属于自己的内存并共享公共数据。

  1. r = Rectangle:new(nil,10,20)

访问属性

我们可以使用点号(.)来访问类的属性:

  1. print(r.length)

访问成员函数

我们可以使用冒号(:)来访问类的属性:

  1. r:printArea()

内存在对象初始化时分配。

完整实例

以下我们演示了 Lua 面向对象的完整实例:

  1. -- Meta class
  2. Shape = {area = 0}
  3.  
  4. -- 基础类方法 new
  5. function Shape:new (o,side)
  6. o = o or {}
  7. setmetatable(o, self)
  8. self.__index = self
  9. side = side or 0
  10. self.area = side*side;
  11. return o
  12. end
  13.  
  14. -- 基础类方法 printArea
  15. function Shape:printArea ()
  16. print("面积为 ",self.area)
  17. end
  18.  
  19. -- 创建对象
  20. myshape = Shape:new(nil,10)
  21.  
  22. myshape:printArea()

执行以上程序,输出结果为:

  1. 面积为 100

Lua 继承

继承是指一个对象直接使用另一对象的属性和方法。可用于扩展基础类的属性和方法。

以下演示了一个简单的继承实例:

  1. -- Meta class
  2. Shape = {area = 0}
  3. -- 基础类方法 new
  4. function Shape:new (o,side)
  5. o = o or {}
  6. setmetatable(o, self)
  7. self.__index = self
  8. side = side or 0
  9. self.area = side*side;
  10. return o
  11. end
  12. -- 基础类方法 printArea
  13. function Shape:printArea ()
  14. print("面积为 ",self.area)
  15. end

接下来的实例,Square 对象继承了 Shape 类:

  1. Square = Shape:new()
  2. -- Derived class method new
  3. function Square:new (o,side)
  4. o = o or Shape:new(o,side)
  5. setmetatable(o, self)
  6. self.__index = self
  7. return o
  8. end

完整实例

以下实例我们继承了一个简单的类,来扩展派生类的方法,派生类中保留了继承类的成员变量和方法:

  1. -- Meta class
  2. Shape = {area = 0}
  3. -- 基础类方法 new
  4. function Shape:new (o,side)
  5. o = o or {}
  6. setmetatable(o, self)
  7. self.__index = self
  8. side = side or 0
  9. self.area = side*side;
  10. return o
  11. end
  12. -- 基础类方法 printArea
  13. function Shape:printArea ()
  14. print("面积为 ",self.area)
  15. end
  16.  
  17. -- 创建对象
  18. myshape = Shape:new(nil,10)
  19. myshape:printArea()
  20.  
  21. Square = Shape:new()
  22. -- 派生类方法 new
  23. function Square:new (o,side)
  24. o = o or Shape:new(o,side)
  25. setmetatable(o, self)
  26. self.__index = self
  27. return o
  28. end
  29.  
  30. -- 派生类方法 printArea
  31. function Square:printArea ()
  32. print("正方形面积为 ",self.area)
  33. end
  34.  
  35. -- 创建对象
  36. mysquare = Square:new(nil,10)
  37. mysquare:printArea()
  38.  
  39. Rectangle = Shape:new()
  40. -- 派生类方法 new
  41. function Rectangle:new (o,length,breadth)
  42. o = o or Shape:new(o)
  43. setmetatable(o, self)
  44. self.__index = self
  45. self.area = length * breadth
  46. return o
  47. end
  48.  
  49. -- 派生类方法 printArea
  50. function Rectangle:printArea ()
  51. print("矩形面积为 ",self.area)
  52. end
  53.  
  54. -- 创建对象
  55. myrectangle = Rectangle:new(nil,10,20)
  56. myrectangle:printArea()

执行以上代码,输出结果为:

  1. 面积为 100
  2. 正方形面积为 100
  3. 矩形面积为 200

函数重写

Lua 中我们可以重写基础类的函数,在派生类中定义自己的实现方式:

  1. -- 派生类方法 printArea
  2. function Square:printArea ()
  3. print("正方形面积 ",self.area)
  4. end