自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

lua實(shí)現(xiàn)面向?qū)ο蟮奶匦?/h1>

開發(fā) 前端
lua本身不支持面向?qū)ο蟮奶匦?,但是由于lua是基于原型(prototype)的語言,要實(shí)現(xiàn)面向?qū)ο蟮奶匦砸彩怯幸欢ǖ姆椒ǖ?,?shí)現(xiàn)方式有很多種, 總結(jié)了一下我最近對(duì)使用lua實(shí)現(xiàn)面向?qū)ο蟮奶匦?/div>

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方法:

  1. --filename:"obj.lua" 
  2. local setmetatablesetmetatable = setmetatable 
  3. module"obj" function inherit (self) 
  4. return function (newclass) 
  5. setmetatable (newclass, self) 
  6. selfself.__index = self   
  7. return newclass   end   
  8. end   
  9. function new (self, o)   
  10. oo = o or {}   
  11. setmetatable (o, self)   
  12. selfself.__index = self   
  13. return o   end   
  14. ----------------------------------------------------------------------   
  15. --Now you can define a new class which extends the previous `obj':   
  16. --filename:"myobj.lua"   
  17. local obj = require"obj"   
  18. module ("myobj", obj:inherit())   
  19. --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方法:

  1. ----------------------------------------------------------------------- 
  2.   --filename:"obj.lua" 
  3.   local setmetatablesetmetatable = setmetatable 
  4.   module"obj" 
  5.   function inherit (self) 
  6.   return function (newclass) 
  7.   newclass = table.clone(self) 
  8.   return newclass 
  9.   end 
  10.   end 
  11.   function new (self, o) 
  12.   oo = o or {} 
  13.   o = table.clone(self) 
  14.   return o 
  15.   end 
  16.   ---------------------------------------------------------------------- 
  17.   --Now you can define a new class which extends the previous `obj': 
  18.  
  19.   --filename:"myobj.lua" 
  20.   local obj = require"obj" 
  21.   module ("myobj", obj:inherit()) 
  22.   --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ì)可以是:

  1. ----------------------------------------------------------------------- 
  2.   --filename:"obj.lua" 
  3.   local setmetatablesetmetatable = setmetatable 
  4.   module"obj" 
  5.   function inherit (self) 
  6.   return function (newclass) 
  7.   newclass = table.clone(self) 
  8.   return newclass 
  9.   end 
  10.   end 
  11.   function new (self, o) 
  12.   oo = o or {} 
  13.   setmetatable (o, self) 
  14.   selfself.__index = self 
  15.   return o 
  16.   end 
  17.   ---------------------------------------------------------------------- 
  18.   --Now you can define a new class which extends the previous `obj': 
  19.  
  20.   --filename:"myobj.lua" 
  21.   local obj = require"obj" 
  22.   module ("myobj", obj:inherit()) 
  23.   --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

責(zé)任編輯:陳四芳 來源: it168.com
相關(guān)推薦

2012-03-13 16:39:52

Java

2009-10-13 14:19:03

VB.NET面向?qū)ο缶?/a>

2009-05-21 09:08:52

接口C++面向?qū)ο?/a>

2022-10-21 09:01:41

StudentC++類型

2023-01-10 09:38:09

面向對(duì)象系統(tǒng)

2011-08-24 09:54:05

Lua字符春交互

2022-08-16 07:57:30

架構(gòu)

2023-04-19 08:43:52

Python面向?qū)ο缶幊?/a>

2009-12-22 01:54:50

C++之父Bjarne Stro

2020-04-15 11:07:31

C語言對(duì)象思想

2021-10-21 18:47:37

JavaScript面向對(duì)象

2013-03-11 09:23:22

Go語言面向?qū)ο?/a>

2009-07-14 16:51:50

Jython中的對(duì)象

2022-10-12 08:38:51

C語言classC++

2009-01-04 09:08:30

面向?qū)ο?/a>繼承接口

2010-07-13 10:47:18

Perl面向?qū)ο?/a>

2010-07-13 14:54:15

Perl面向?qū)ο缶幊?/a>

2022-07-30 23:41:53

面向過程面向?qū)ο?/a>面向協(xié)議編程

2021-05-20 08:54:16

Go面向對(duì)象

2024-04-02 07:32:58

Go語言接口
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)