lua 学习笔记9

lua 学习笔记9

/******************************/
    面向对象基础–生成实例
/******************************/
在lua中,理所当然是利用table来实现oo的。
先看看以前的用法,如果用table来实现一个函数,函数里面要操作表格的一些数据。
    Account = {balance = 0}
    function Account.withdraw (v)
      Account.balance = Account.balance – v
    end
这里,总是要引用"Account."才能访问到内部的变量,如果把它当成一个类,这样做就没有办法生成对象,失去了oo的原则,那么,怎么做呢?
第一种方法是在函数里面加上一个参数,这个参数指向当前的表格,那么访问时就灵活多了。例如:
    funciton Account.p(tableName , v)
      tableName.balance = tableName.balance – v
    end
第二种方法跟第一种一样,不过利用lua中内置的colon operator(实际上只是把类名这个参数省略掉了),例如:
    function Account:withdraw (v)
      self.balance = self.balance – v
    end
当调用时,直接使用"实例名:widthdraw(v)"就可以自动把当前的实例名当作参数传导函数里面的self里。
需要注意的是,在这样的函数里,只能使用Account和self指向隐藏的参数类名
下面我写了一个例子,试试看生成一个实例的用法:
classTest={}
classTest.number = 1
function classTest:addThenPrint(value)
 self.number = self.number + value
 print(self.number)
end
function getAClass(o)
 o= o or {}
 setmetatable(o,classTest)
 classTest.__index=classTest
 return o
end
t = getAClass({number = 10})
t:addThenPrint(1)           
t1 = getAClass({number = 20})
t1:addThenPrint(1)
print(t.number)
print(t1.number)
–[[
output:
11
21
11
21
–]]
更进一步,可以把上面的程序改成:
function classTest:getInstance(o)
 if(o == nil) then o = {} end
 if(type(o) ~= "table") then error("error in parameter!") end
 setmetatable(o,self)
 self.__index=self
 return o
end
t2 = classTest:getInstance({number = 30})
t2:addThenPrint(1)
这样,就可以直接通过表格(把表格当成类)来生成实例了。
/*************/
    继承
/*************/
通过上面的方法创建了类,那么继承就变得很简单了.
直接采用上述的方法创建一个类,再用这个类创建一个实例,这个实例再创建一个这个实例的实例,就完成了继承的实例( 好累) ,例如:
    SpecialAccount = Account:new()
Up to now, SpecialAccount is just an instance of Account. The nice thing happens now:
    s = SpecialAccount:new{limit=1000.00}
添加到上述的"面向对象—生成实例"例子中如下代码:
function t2:addThenPrint(v)
 print("Hello")
end
t2:addThenPrint(1)
看看继承之后的效果.
/*************/
    私有化
/*************/
看看这个例子就明白里.
   function newAccount (initialBalance)
      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
/*********************/
    弱表(weak table)
/*********************/
Weak tables are the mechanism that you use to tell Lua that a reference should not prevent the reclamation of an object. A weak reference is a reference to an object that is not considered by the garbage collector. If all references pointing to an object are weak, the object is collected and somehow these weak references are deleted. Lua implements weak references as weak tables: A weak table is a table where all references are weak. That means that, if an object is only held inside weak tables, Lua will collect the object eventually.
Tables have keys and values and both may contain any kind of object. Under normal circumstances, the garbage collector does not collect objects that appear as keys or as values of an accessible table. That is, both keys and values are strong references, as they prevent the reclamation of objects to which they refer. In a weak table, keys and values may be weak. That means that there are three kinds of weak tables: tables with weak keys, tables with weak values, and fully weak tables, where both keys and values are weak. Irrespective of the table kind, when a key or a value is collected the whole entry disappears from the table.
The weakness of a table is given by the field __mode of its metatable. The value of this field, when present, should be a string: If the string contains the letter `k′ (lower case), the keys in the table are weak; if the string contains the letter `v′ (lower case), the values in the table are weak. The following example, although artificial, illustrates the basic behavior of weak tables:
    a = {}
    b = {}
    setmetatable(a, b)
    b.__mode = "k"         — now `a’ has weak keys
    key = {}               — creates first key
    a[key] = 1
    key = {}               — creates second key
    a[key] = 2
    collectgarbage()       — forces a garbage collection cycle
    for k, v in pairs(a) do print(v) end
      –> 2
In this example, the second assignment key = {} overwrites the first key. When the collector runs, there is no other reference to the first key, so it is collected and the corresponding entry in the table is removed. The second key, however, is still anchored in variable key, so it is not collected.
记住,有几个东西是弱表也不会清的,他们是numbers booleans String

2 thoughts on “lua 学习笔记9

  1. Hi,Do you have used LCDs, used flat screens and secondhand LCDs? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels and working for LCD recycling.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels.
    Contact Us
    E-mail:sstar@netvigator.com
    website:www.sstar-hk.com[i

  2. wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling -229109331540896

Leave a Reply

Your email address will not be published. Required fields are marked *