counter = {
 count = 0
}
function counter.get(self)
 return self.count
end

function counter:inc()
 self.count=self.count+1
end

print(counter.get(counter))
counter.inc(counter)
print(counter.get(counter))

counter2={
 count=4,
 get = counter.get,
 inc = counter.inc,
}

print(counter2:get())
counter.inc(counter2)
print(counter2.get(counter2))

print()

tb1 ={ "alpha","beta","gamma"}
mt={}
setmetatable(tb1,mt)

print(getmetatable(tb1) == mt)

print()

function mt.__add(a,b)
 local result = setmetatable({},mt)
 for i=1,#a do
 table.insert(result,a[i])
 end
 for i=1,#b do
 table.insert(result,b[i])
 end
 return result
end

tb2={"aaa","bbb","ccc"}
res=tb1+tb2
for i,v in ipairs(res) do
 print(v)
end
print()
function mt.__unm(a)
 local result = setmetatable({},mt)
 for i=#a , 1 ,-1 do
 table.insert(result,a[i])
 end
 return result
end

res=-tb1+tb2
for i,v in ipairs(res) do
 print(v)
end

print()
function mt.__tostring(a)
 local result = "{"
 for i,v in ipairs(a) do
 result = result.." "..v
 end
 result = result.." } "
 return result
end

print(tb1)

function mt.__index(tb1,key)
 print("there is no "..key.." in the table")
 return nil
end

print(tb1["fsy"])

function mt.__newindex(a,key,v)
 if( key == "haha") then
 error(" Stop laugh!",2)
 else
 rawset(a,key,v)
 end
end

tb1.haha="heihei"

 
运行结果:

0
1
4
5

true

alpha
beta
gamma
aaa
bbb
ccc

gamma
beta
alpha
aaa
bbb
ccc

{ alpha beta gamma } 
there is no fsy in the table
nil
lua: my_test.lua:166: Stop laugh!
stack traceback:
 [C]: in function 'error'
 my_test.lua:160: in function <my_test.lua:158>
 my_test.lua:166: in main chunk
 [C]: ?

 

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。