--------------------------------------------------------------------------------------- baseclass={} function baseclass:new (o) o = o or {} -- create object if user does not provide one o.super=baseclass -- keep base class setmetatable(o, self) self.__index = self return o end function baseclass:classname() return "baseclass" end function baseclass:ctor() return "baseclassctor" end a1class=baseclass:new() a1class.classname() function a1class:classname() return "a1class" end print (a1class.classname()=="a1class")-- check overwrite print (a1class.ctor()) --check Inheritance --print (a1class.super:classname()) ---------------------------------------------------------------------------------------
------------------------------------------------------ --V2 ------------------------------------------------------ baseclass={} baseclass.level=0 baseclass.__index=baseclass function baseclass:new (o) o = o or {} -- create object if user does not provide one o.super=self -- keep base class setmetatable(o, {__index=function(tablename,keyname) -- two way to do it return self[keyname] -- or return o.super[keyname] end } ) o.__index=o --self.__index = self return o end function baseclass:classname() return "baseclass" end function baseclass:ctor() return "baseclassctor" end ------------------------------------------------------ a1class=baseclass:new() a1class.classname() function a1class:classname() return "a1class" end --[[ function a1class:new (o) o = o or {} -- create object if user does not provide one o.super=self -- keep base class setmetatable(o, {__index=function(tablename,keyname) return self[keyname] end } ) o.__index=o --self.__index = self return o end --]] --- print (a1class:classname()=="a1class")-- check overwrite print (a1class.level==0)-- check base class 's property print (a1class:ctor()=="baseclassctor") --check Inheritance print ("-----------b-----------") b1Class=a1class:new() print (b1Class:ctor()=="baseclassctor") print (b1Class:classname()==a1class:classname())--check ------------------------------------------------------
------------------------------------------------------ --V3 ------------------------------------------------------ baseclass={} baseclass.level=0 baseclass.__index=baseclass function baseclass:new (o) o = o or {} -- create object if user does not provide one o.super=self -- keep base class setmetatable(o, {__index=function(tablename,keyname) -- two way to do it print ("invoke func "..keyname.. " from "..tostring(tablename).." now is "..tostring(self) ) func= self[keyname] if func then o[keyname]=func --print ("keep it "..keyname.." in "..tostring(self)) end return func -- or return o.super[keyname] end } ) o.__index=o --self.__index = self return o end function baseclass:classname() return "baseclass" end function baseclass:ctor() return "baseclassctor" end ------------------------------------------------------ a1class=baseclass:new() a1class.classname() function a1class:classname() return "a1class" end print (a1class:classname()=="a1class")-- check overwrite print (a1class.level==0)-- check base class 's property --print (a1class:ctor()=="baseclassctor") --check Inheritance b1Class=a1class:new() function b1Class:classname() return "b1Class" end --print (b1Class:ctor()=="baseclassctor") print (b1Class:classname()~=a1class:classname())--check print (b1Class.level==0)-- check base class 's property print ("--------check drived class search") print (b1Class:ctor())-- check base class 's property print (b1Class:ctor())-- check base class 's property ------------------------------------------------------ print ("baseclass is "..tostring(baseclass)) print ("a1class is "..tostring(a1class)) print ("b1Class is "..tostring(b1Class))
第四版:替代new的方式直接一个函数生成类 drivedclass==class(baseclass) 1.直接替代call方法 2.function 方式 ------------------------------------------------------ --V4 ------------------------------------------------------ function class (baseclass) local o = {} o.super=baseclass -- keep base class setmetatable(o, {__index=function(tablename,keyname) -- print ("invoke func "..keyname.. -- " from "..tostring(tablename).." now is "..tostring(baseclass) ) func= baseclass[keyname] if func then o[keyname]=func --print ("keep it "..keyname.." in "..tostring(self)) end return func -- or return o.super[keyname] end } ) return o end ------------------------------------------------------ baseclass=class() baseclass.level=0 function baseclass:classname() return "baseclass" end function baseclass:ctor() return "baseclassctor" end a1class=class(baseclass) function a1class:classname() return "a1class" end print (a1class:classname()=="a1class")-- check overwrite print (a1class.level==0)-- check base class 's property print (a1class:ctor())-- check base class 's property --print (a1class:ctor()=="baseclassctor") --check Inheritance b1Class=class(a1class) --function b1Class:classname() return "b1Class" end print (b1Class:classname())-- y ------------------------------------------------------end
------------------------------------------------------ --V5 ------------------------------------------------------ --o = {} local indexlevel=0 ----------------------------- function class (baseclass) local o = {} o.indexlevel=0 o.super=baseclass -- keep base class setmetatable(o, {__index=function(tablename,keyname) print ("invoke func "..keyname.. " from "..tostring(tablename).." now is "..tostring(baseclass) ) print (" enter indexlevel: "..indexlevel) indexlevel=indexlevel+1 func= baseclass[keyname] indexlevel= indexlevel-1 print (" leave indexlevel: "..indexlevel) if indexlevel==0 then if func then o[keyname]=func print ("keep it "..keyname.." in "..tostring(baseclass)) end end return func end } ) --- o.tablefunctions={} return o end ------------------------------------------------------ baseclass=class() baseclass.level=0 function baseclass:classname() return "baseclass" end function baseclass:ctor() return "baseclassctor" end a1class=class(baseclass) function a1class:classname() return "a1class" end b1Class=class(a1class) print ("--------check drived class search") print (b1Class:ctor())-- check base class 's property print (b1Class:ctor())-- check base class 's property ------------------------------------------------------ print ("-------------------------------") print ("baseclass is "..tostring(baseclass)) print ("a1class is "..tostring(a1class)) print ("b1Class is "..tostring(b1Class))
------------------------------------------------------ --V6 ------------------------------------------------------ ------------------------------- --o = {} -- local indexlevel=0 local _class={} -- store global var ----------------------------- function class (baseclass) local o = {} o.super=baseclass -- store base class _class[o]={} --存储新到的方法 local storeNewFunc=function(tablename,key,value) print ("set func "..key.." in "..tostring(tablename).." v is "..tostring(value) ) _class[o][key]=value--store new func into o end -- if base exist then add func lookup local searchFunc=function(t,key) local func=_class[t][key] if not func and baseclass then print (" search baseclass "..key) func=baseclass[key] if func then o[key]=value end end if func then print (" func "..key.." found ") else print (" func "..key.." nof found ") end return func end setmetatable(o,{__index=searchFunc,__newindex=storeNewFunc }) return o end ------------------------------------------------------ baseclass=class() print ("-------------------------------") function baseclass:classname() return "baseclass" end print ("-------------------------------") --print ("classname= "..baseclass:classname())-- check overwrite print ("-------------------------------") --print ("classname= "..baseclass:classname())-- check overwrite function baseclass:ctor() return "baseclass_ctor" end a1class=class(baseclass) function a1class:classname() return "a1class" end print ("---test class----------------------------") print (a1class:classname()=="a1class")-- check overwrite print (a1class:ctor()=="baseclass_ctor")-- check overwrite print ("---test class 1----------------------------") b1Class=class(a1class) print ("---test class 2----------------------------") print (b1Class:ctor()) print ("---test class 3----------------------------") print (b1Class:ctor()) ------------------------------------------------------ print ("-------------------------------") print ("baseclass is "..tostring(baseclass)) print ("a1class is "..tostring(a1class)) print ("b1Class is "..tostring(b1Class))
local _class={} ----------------------------- function class (baseclass) local o = {} o.super=baseclass -- store base class ,not nessary _class[o]={} o.new = function (...) local instance={}--instance setmetatable(instance,{__index=o,__newindex=o}) --setmetatable(instance,{__index=searchFunc,__newindex=storeNewFunc }) return instance end --存储新到的方法 local storeNewFunc=function(tablename,key,value) print ("set func "..key.." in "..tostring(tablename).." v is "..tostring(value) ) _class[o][key]=value--store new func into o end --方法查找 if base exist then add func lookup local searchFunc=function(t,key) local func=_class[t][key] if not func and baseclass then print (" search baseclass "..key) func=baseclass[key] if func then o[key]=func end end if func then print (" func "..key.." found ") else print (" func "..key.." nof found ") end return func end setmetatable(o,{__index=searchFunc,__newindex=storeNewFunc }) return o end ------------------------------------------------------ baseclass=class() print ("-------------------------------") function baseclass:classname() return "baseclass" end print ("-------------------------------") --print ("classname= "..baseclass:classname())-- check overwrite print ("-------------------------------") --print ("classname= "..baseclass:classname())-- check overwrite --dump(_class) function baseclass:ctor() return "baseclass_ctor" end a1class=class(baseclass) function a1class:classname() return "a1class" end b1Class=class(a1class) bb1=b1Class.new() print ("---test class 5----------------------------") print (bb1:ctor()) print (bb1:ctor()) print (bb1:ctor())