Lua中实现Switch功能

熟悉Lua的大家应该都知道,Lua中是没有Switch语法的,所以很多时候要条件判断的时候只有一条又一条的if 和elseif 去写,但是很多时候看上去很不直观,但是Lua的table类型其实相当强大。利用表结构和匿名函数,我们就可以实现Switch功能。

以下是在项目中的一段应用代码:

function UIActivityTemp.SetPageUIInfoWithIndex(index)--根据索引设置子页面UI信息
	local switch = {
		[1] = function()
			if this.IsActivityOpen(LocalActivityType.SingleCharge) then
				this.SetPlayerSingleChargeDiamondUIInfo(this.activityPageGrid)
			end
		end,
		[2] = function()
			if this.IsActivityOpen(LocalActivityType.DailyAccumulateChargeMoney) then
				this.SetPlayerDailyAccumulateChargeMoneyUIInfo(this.activityPageGrid)
			end
		end,
		[3] = function()
			if this.IsActivityOpen(LocalActivityType.AccumulateChargeMoney) then
				this.SetPlayerAccumulateChargeMoneyUIInfo(this.activityPageGrid)
			end
		end,
		[4] = function()
			if this.IsActivityOpen(LocalActivityType.DailyAccumulateCostDiamond) then
				this.SetPlayerDailyAccumulateCostDiamondUIInfo(this.activityPageGrid)
			end
		end,
		[5] = function()
			if this.IsActivityOpen(LocalActivityType.AccumulateCostDiamond) then
				this.SetPlayerAccumulateCostDiamondUIInfo(this.activityPageGrid)
			end
		end,
		[6] = function()
			if this.IsActivityOpen(LocalActivityType.UpgradeGift) then
				this.SetPlayerUpgradeGiftUIInfo(this.activityPageGrid)
			end
		end,
		[7] = function()
			if this.IsActivityOpen(LocalActivityType.VipBuyGift1) then
				this.SetPlayerVipGift1UIInfo(this.activityPageGrid)
			end
		end,
		[8] = function()
			if this.IsActivityOpen(LocalActivityType.VipBuyGift2) then
				this.SetPlayerVipBuyGift2UIInfo(this.activityPageGrid)
			end
		end,
	}
	local setUIFunc = switch[index]
	if setUIFunc then
		setUIFunc()
	end
end

代码中我们可以看到,我们把匿名函数存储在表中,根据外部参数来运行指定索引的函数,从而实现了Switch功能。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,我们可以了解到Lua没有类似于JavaScript的splice函数。但是,我们可以使用Lua的table库来实现类似于splice函数的功能。下面是一个简单的Lua函数,可以实现类似于JavaScriptsplice函数的功能: ```lua function splice(t, start, deleteCount, ...) local len = #t start = start or 1 if start < 0 then start = len + start + 1 end deleteCount = deleteCount or (len - start + 1) local removed = {} for i = start, start + deleteCount - 1 do table.insert(removed, t[i]) end local args = {...} local addCount = #args local newLen = len - deleteCount + addCount for i = len, start + deleteCount - 1, -1 do t[i + addCount] = t[i] end for i = 1, addCount do t[start + i - 1] = args[i] end for i = len, newLen + 1, -1 do t[i] = nil end return removed end ``` 这个函数接受一个table作为第一个参数,表示要操作的数组;第二个参数表示要删除或插入的起始位置;第三个参数表示要删除的元素个数;后面的参数表示要插入的元素。函数返回一个数组,表示被删除的元素。 下面是一个使用示例: ```lua local t = {1, 2, 3, 4, 5} local removed = splice(t, 2, 2, 6, 7) print(table.concat(t, ", ")) -- 输出:1, 6, 7, 5 print(table.concat(removed, ", ")) -- 输出:2, 3 ``` 这个示例,我们将数组t从第2个位置开始的2个元素删除,并在这个位置插入了两个新元素6和7。最后,我们打印了修改后的数组t和被删除的元素数组removed。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值