lua實(shí)現(xiàn)面向?qū)ο蟮奶匦?/h1>
lua本身不支持面向?qū)ο蟮奶匦?,但是由于lua是基于原型(prototype)的語言,要實(shí)現(xiàn)面向?qū)ο蟮奶匦砸彩怯幸欢ǖ姆椒ǖ模瑢?shí)現(xiàn)方式有很多種, 總結(jié)了一下我最近對(duì)使用lua實(shí)現(xiàn)面向?qū)ο蟮奶匦?,主要可以分為以下兩種不同的方式來實(shí)現(xiàn):
1、使用metatable的__index域?qū)崿F(xiàn),實(shí)現(xiàn)的時(shí)候需要利用的lua語言的一些特性才可以實(shí)現(xiàn),主要有:
a、將table b作為table a的一個(gè)prototype的方法:setmetatable(a, {__index = b});
b、lua5.1中實(shí)現(xiàn)的module機(jī)制;
通過這兩個(gè)技術(shù)你可以實(shí)現(xiàn)一個(gè)基礎(chǔ)類,實(shí)現(xiàn)類的inherit和new方法:
- --filename:"obj.lua"
- local setmetatablesetmetatable = setmetatable
- module"obj" function inherit (self)
- return function (newclass)
- setmetatable (newclass, self)
- selfself.__index = self
- return newclass end
- end
- function new (self, o)
- oo = o or {}
- setmetatable (o, self)
- selfself.__index = self
- return o end
- ----------------------------------------------------------------------
- --Now you can define a new class which extends the previous `obj':
- --filename:"myobj.lua"
- local obj = require"obj"
- module ("myobj", obj:inherit())
- --Class `myobj' will "inherit" the methods `new' and `inherit' from class `obj'.
優(yōu)點(diǎn):
1、由于子類的很多數(shù)據(jù)和方法都是共用了父類的,用到父類的數(shù)據(jù)和方法的時(shí)候,只是在使用的時(shí)候才直接調(diào)用父類的方法和數(shù)據(jù),這樣可以減少程序內(nèi)存的消耗,更主要的是,父類在運(yùn)行期的修改是會(huì)影響到子類的;
2、充分利用了lua語言的特性,父類的方法和數(shù)據(jù)的訪問是解析器來做的,所以效率上的開銷還是比較小的;
缺點(diǎn):
1、如果父類中有一個(gè)數(shù)據(jù)是一個(gè)引用的時(shí)候(如table)的時(shí)候,就會(huì)出現(xiàn)在一個(gè)子類中操作這個(gè)table會(huì)改變其他子類的情況,造數(shù)據(jù)的不 一致,所以應(yīng)該盡量避免這種類的創(chuàng)建,如果有這樣的需求的話,就需要對(duì)inherit和new函數(shù)進(jìn)行一些特殊的操作,簡單來說就是加一個(gè)init函數(shù), 將所有這類的數(shù)據(jù)都顯示的創(chuàng)建一下。
2、由于每次取操作都需要在metatable中取,所以,每次就會(huì)增加一層繼承,就增加一個(gè)函數(shù)調(diào)用的開銷,雖然是由解析器來做的,但是如果層次多了的話,還是有開銷的;
3、使用table拷貝的方式實(shí)現(xiàn),實(shí)現(xiàn)的時(shí)候利用的lua的技術(shù)為:
a、使用lua實(shí)現(xiàn)一個(gè)table拷貝的函數(shù);
b、lua5.1中實(shí)現(xiàn)的module機(jī)制;
通過這兩個(gè)技術(shù)你可以實(shí)現(xiàn)一個(gè)基礎(chǔ)類,實(shí)現(xiàn)類的inherit和new方法:
- -----------------------------------------------------------------------
- --filename:"obj.lua"
- local setmetatablesetmetatable = setmetatable
- module"obj"
- function inherit (self)
- return function (newclass)
- newclass = table.clone(self)
- return newclass
- end
- end
- function new (self, o)
- oo = o or {}
- o = table.clone(self)
- return o
- end
- ----------------------------------------------------------------------
- --Now you can define a new class which extends the previous `obj':
- --filename:"myobj.lua"
- local obj = require"obj"
- module ("myobj", obj:inherit())
- --Class `myobj' will "inherit" the methods `new' and `inherit' from class `obj'.
優(yōu)點(diǎn):
1、父類中的數(shù)據(jù)是全部拷貝到子類中的,所以,不存在數(shù)據(jù)不一致的情況;
2、所有的函數(shù)調(diào)用和數(shù)據(jù)調(diào)用都是直接調(diào)用每個(gè)實(shí)例的,不需要到父類中查找;
缺點(diǎn):
1、全部數(shù)據(jù)的copy,在創(chuàng)建的時(shí)候就會(huì)增加一個(gè)table copy的過程,影響效率;
2、全部數(shù)據(jù)和方法都是在創(chuàng)建的時(shí)候拷貝一份的,會(huì)增加很多的內(nèi)存消耗,而且如果在運(yùn)行期改變了父類,并不能改變子類;
總結(jié):
結(jié)合這兩種方式的有缺點(diǎn),從一個(gè)面向?qū)ο蟮慕嵌葋碚f,第一種方式更加適合實(shí)現(xiàn)面向?qū)ο蟮奶匦?,第二種方式對(duì)面向?qū)ο蟮哪M就牽強(qiáng)一些(缺點(diǎn) 2),但是從使用的角度來說,因?yàn)樵谠L問數(shù)據(jù)和方法速度上,第二種方式還是有優(yōu)勢的,所以,在具體的使用的時(shí)候,可以靈活一下使用,將兩者結(jié)合一下。
比如說,對(duì)于客戶端這邊來說,類在開始創(chuàng)建好了以后就一般不需要修改了,而且子類一般都是需要父類的所有的方法和數(shù)據(jù)的,所有我們就可以使用第 二種方式,而生成對(duì)象實(shí)例的時(shí)候,對(duì)象的實(shí)例一般都不會(huì)調(diào)用類的所有的方法,而且用完了這個(gè)實(shí)例,就會(huì)銷毀的,所以,我們這里就可以采用第一種方式,結(jié)合 一下設(shè)計(jì)可以是:
- -----------------------------------------------------------------------
- --filename:"obj.lua"
- local setmetatablesetmetatable = setmetatable
- module"obj"
- function inherit (self)
- return function (newclass)
- newclass = table.clone(self)
- return newclass
- end
- end
- function new (self, o)
- oo = o or {}
- setmetatable (o, self)
- selfself.__index = self
- return o
- end
- ----------------------------------------------------------------------
- --Now you can define a new class which extends the previous `obj':
- --filename:"myobj.lua"
- local obj = require"obj"
- module ("myobj", obj:inherit())
- --Class `myobj' will "inherit" the methods `new' and `inherit' from class `obj'.
這里的關(guān)鍵是繼承是copy出來的,而實(shí)例是采用metatable的方式實(shí)現(xiàn)的。
原文鏈接:http://tech.it168.com/j/2008-02-17/200802171004466.shtml






