在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):kikito/stateful.lua开源软件地址(OpenSource Url):https://github.com/kikito/stateful.lua开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):Stateful
Examplelocal class = require 'middleclass'
local Stateful = require 'stateful'
local Enemy = class('Enemy')
Enemy:include(Stateful)
function Enemy:initialize(health)
self.health = health
end
function Enemy:speak()
return 'My health is' .. tostring(self.health)
end
local Immortal = Enemy:addState('Immortal')
-- overriden function
function Immortal:speak()
return 'I am UNBREAKABLE!!'
end
-- added function
function Immortal:die()
return 'I can not die now!'
end
local peter = Enemy:new(10)
peter:speak() -- My health is 10
peter:gotoState('Immortal')
peter:speak() -- I am UNBREAKABLE!!
peter:die() -- I can not die now!
peter:gotoState(nil)
peter:speak() -- My health is 10 InstallationFirst, make sure that you have downloaded and installed middleclass Just copy the stateful.lua file wherever you want it (for example on a lib/ folder). Then write this in any Lua file where you want to use it: local class = require 'middleclass'
local Stateful = require 'stateful' The SpecsThis project uses busted for its specs. In order to run them, install busted, and then execute it on the top folder:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论