Lua基础:面向对象程序设计

示例代码:

  1. Accout={balance=0}             
  2.  
  3. function Accout.withdraw(v)
  4.     Accout.balance=Accout.balance-v
  5. end
  6.  
  7. Accout.withdraw(100.00)   --使用 调用方法
  8.  
  9. function Accout.withdraw(self,v)   --self或者this,表示方法作用的对象
  10.     self.balance=self.balance-v
  11. end
  12.  
  13. a1=Accout;Accout=nil
  14. a1.withdraw(a1,100.00)   --对象 . 方法
  15. -------------------------------------------------------------------
  16. Account = {
  17.     balance=0,
  18.     withdraw = function (self, v)
  19.         self.balance = self.balance - v
  20.     end
  21. }
  22. function Account deposit (v)            -- :隐藏self参数
  23.     self.balance = self.balance + v
  24. end
  25. Account.deposit(Account, 200.00)
  26. Account:withdraw(100.00)  -- 使用 调用方法

类:面向对象的语言中提供了类的概念,作为创建对象的模板。对象是类的实例。在Lua中每个对象都有一个 prototype(原                型),当调用不属于对象的某些操作时,会最先会到 prototype 中查找这些操作。

  1. Account = {
  2.  balance=0,
  3.  withdraw = function (self, v)
  4.  self.balance = self.balance - v
  5. end
  6. }
  7.  
  8. function Account:deposit (v)
  9.  self.balance = self.balance + v
  10. end
  11.  
  12. function Account:new (o)
  13.  o = o or {} -- create object if user does not provide one
  14.  setmetatable(o, self)
  15.  self.__index = self
  16. return o
  17. end
  18.  
  19. a = Account:new{balance = 0}
  20. a:deposit(100.00)
  21. ------------------------------------------------等价于
  22. Account.deposit(a, 100.00)
  23. b = Account:new()
  24. print(b.balance)  --等价于 b.balance = b.balance + v

继承:类可以访问其他类的方法

  1. Account = {balance = 0}           --基类
  2.  
  3. function Account:new (o)
  4.     o = o or {}
  5.     setmetatable(o, self)
  6.     self.__index = self
  7.    return o
  8. end
  9.  
  10. function Account:deposit (v)
  11.     self.balance = self.balance + v
  12. end
  13.  
  14. function Account:withdraw (v)
  15.    if v > self.balance then error"insufficient funds" end
  16.    self.balance = self.balance - v
  17. end
  18.  
  19. SpecialAccount = Account:new()       --子类继承基类的所有操作
  20. s = SpecialAccount:new{limit=1000.00}
  21. s:deposit(100.00)
  22.  
  23. function SpecialAccount:withdraw (v)       --重写父类方法
  24.    if v - self.balance >= self:getLimit() then
  25.        error"insufficient funds"
  26.    end
  27.     self.balance = self.balance - v
  28. end
  29.  
  30. function SpecialAccount:getLimit ()
  31.     return self.limit or 0
  32. end
  33.  
  34. function s:getLimit ()
  35.     return self.balance * 0.10
  36. end

多重继承:

  1. Account = {                                 --类Account
  2.     balance=0,
  3.     withdraw = function (self, v)
  4.     self.balance = self.balance - v
  5. end
  6. }
  7.  
  8. local function search (k, plist)  --在列表plist中查找k
  9.     for i=1, table.getn(plist) do
  10.         local v = plist[i][k]               
  11.         if v then return v end
  12.     end
  13. end
  14.  
  15. function createClass (...)       --创建新类
  16.     local c = {}                            --新类
  17.     setmetatable(c, {__index = function (t, k)      --搜索列表中的每一个方法
  18.        local v = search(k, arg)
  19.        t[k] = v                                            -- 保存供下次访问
  20.        return v
  21.     end})
  22.         c.__index = c        --C作为_index metamethod的实例
  23.   function c:new (o)        --为新类定义构造
  24.     o = o or {}
  25.     setmetatable(o, c)
  26.     return o
  27.     end
  28.   return c          --返回新类
  29. end
  30.  
  31. Named = {}   --类
  32.  
  33. function Named:getname ()  --类的getname()方法
  34.     return self.name
  35. end
  36.  
  37. function Named:setname (n) --类的setname()方法
  38.     self.name = n
  39. end
  40.  
  41. NamedAccount = createClass(Account, Named)     --创建一个类同时继承Account,Named
  42. account = NamedAccount:new{name = "Paul"}        --子类的实例
  43. print(account:getname())                                             

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/88083512
今日推荐