--jingz 方法一local TaskManager = class("TaskManager",super={})function TaskManager:ctor() self.currentTaskId = nilend function TaskManager:setCurrentTaskId(taskId) if taskId then self.currentTaskId = taskId return true else return nil end end function TaskManager:getCurrentTask() return self.currentTaskId end function TaskManager:getCurrentTaskId() return self.currentTaskId end function TaskManager:setCurrentTask(taskId) if taskId then self.currentTaskId = taskId return true else return nil end end return TaskManager
方法二:自定义
--jingzlocal TaskManager = {}function TaskManager:create() local self = setmetatable(TaskManager,self) self.__index = self self.currentTaskId = nil return selfend function TaskManager:setCurrentTaskId(taskId) if taskId then self.currentTaskId = taskId return true else return nil end end function TaskManager:getCurrentTask() return self.currentTaskId end function TaskManager:getCurrentTaskId() return self.currentTaskId end function TaskManager:setCurrentTask(taskId) if taskId then self.currentTaskId = taskId return true else return nil end endreturn TaskManager
无论方法一、方法二中:记得最后一句:
return TaskManager
使用时同时要记得require该模块
local TaskManager = require("app.view.main.TaskManager") local a = TaskManager:create() a:setCurrentTask("100001") print(a:getCurrentTask())
基本上,这样子就实现了lua的类基础机制,然后开始我们C++lua之路把