Lua封装map

来源:互联网 发布:软件行业编程工具 编辑:程序博客网 时间:2024/06/05 05:05
map = {}local this = mapfunction this:new()    o = {}    setmetatable(o,self)    self.__index = self    self.count = 0    return oendfunction this:insert(k,v)    if nil == self[k] then        --table.insert(self,{a = b})        self[k] = v        self.count = self.count + 1        print("insert")    end    print("insert end")endfunction this:remove(k)    if nil ~= self[k] then        self[k] = nil        if self.count >0 then            self.count = self.count - 1        end    endendfunction this:getpair(k)    local value = nil    if nil ~= self[k] then        value = self[k]        print("getpair")    end    return valueendfunction this:clear()    for k,_ in pairs(self) do        if nil ~= self[k] then            self[k] = nil        end    end    self.count = 0endlocal characters = map:new()characters:insert("name1"," this Name:123")characters:remove("name1")characters:insert("name2","this Name:2")local name2 = characters:getpair("name2")print(name2)print(characters.count)for k,v in pairs(characters) do    print(k,v)end

这里写图片描述

0 0
原创粉丝点击