当前位置:网站首页>闭包问题C# Lua
闭包问题C# Lua
2022-06-26 06:21:00 【ThomasQxx】
闭包问题汇总(之前有个大佬时不时问几个问题,现在来总结下经验)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ToggleTest : MonoBehaviour
{
public Toggle[] toggles;
private void Start() {
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].onValueChanged.AddListener((isOn)=>OnToggleChanged(isOn,i));
}
}
private void OnToggleChanged(bool isOn,int index){
Debug.Log($"index==>{
index}..State===>{
isOn}");
}
}
看到这里可以先猜猜选择Toggle打印结果会是多少?Toggle单选按钮有三个。
可以看到Index始终为3。。。
为什么结果会是3呢?这里就会提到闭包这个概念:内层的函数可以引用包含在它外层的函数的变量,即使外层函数的执行已经终止。但该变量提供的值并非变量创建时的值,而是在父函数范围内的最终值。正常来讲For循环执行完毕的时候i变量的内存会随着For循环结束而结束生命。但是由于匿名函数引用了i,导致i不能随For循环结束而结束。所以这个时候i=3.当我们执行点击的时候index=3.这里问题就找到了,那么应该如何解决呢?
解决方案
这样就解决了。想要弄清楚解决的思路,还是得从内存谈起。上一张没有int t = i的代码中。i有几个内存地址呢?可以看出来i只有一个内存地址。而当前图中t有几个内存地址呢?很明显有三个,每次For循环进来都会声明一个t的内存地址。并且t的值随着For循环结束都是不同的。分别是0,1,2。所以问题到这里就可以完全理解了。
Lua中的闭包
function newCounter()
local i = 0
return function()
i = i + 1
return i
end
end
c1 = newCounter()
print(c1())
print(c1())
结果应该是1,2.i本来应该在NewCounter调用结束时候就随之释放内存。但是由于内部函数引用,所以内存继续存在,第一次执行完c1()i = 0+1 = 1;第二次执行c1()完i就应该为i = 1+1 =2;
function newCounter()
local i = 0
return function()
i = i + 1
return i
end
end
c1 = newCounter()
print(c1())
c1 = nil
c2 = newCounter()
print(c2())
结果应该是1,1.因为第一次执行完c1()i = i + 1 = 0 + 1 = 1;但是接下来c1=nil等于释放了内存。c2 = newCounter() 执行c2()时候这个时候i为0.所以c2()结果为i = i + 1 = 0 + 1 = 1;所以最终结果为1,1.
边栏推荐
- 3.pyinstaller module introduction
- DPDK——TCP/UDP协议栈服务端实现(一)
- Market trend report, technical innovation and market forecast of microencapsulated chemical pesticides in China
- typescript的class结合接口(interface)的简单使用
- 事务与消息语义
- 连接数服务器数据库报:错误号码2003Can‘t connect to MySQL server on ‘服务器地址‘(10061)
- Transaction and message semantics
- Use the fast proxy to build your own proxy pool (mom doesn't have to worry about IP being blocked anymore)
- Play with a variety of application scenarios and share secrets with Kwai MMU
- [spark] how to implement spark SQL field blood relationship
猜你喜欢
Design and practice of low code real-time data warehouse construction system
Everything is a vector. The service practice of iqiyi online vector recall project
Mysql-10 (key)
Logstash - logstash pushes data to redis
Tencent's 2022 school recruitment of large factories started with salary, and the general contracting of cabbage is close to 40W!
Logstash -- send an alert message to the nail using the throttle filter
Play with a variety of application scenarios and share secrets with Kwai MMU
How to select and build a real-time data warehouse scheme
Hot! 11 popular open source Devops tools in 2021!
MYSQL索引不生效的原因
随机推荐
Transformer中的Self-Attention以及Multi-Head Self-Attention(MSA)
消息队列-消息事务管理对比
Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction
个人博客系统需求分析
302. minimum rectangular BFS with all black pixels
Container with the most water
Gof23 - prototype mode
Multi thread synchronous downloading of network pictures
China micro cultivator market trend report, technical dynamic innovation and market forecast
Mysql-10 (key)
【微服务系列】Protocol buffer动态解析
MYSQL索引不生效的原因
Typora activation method
The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
Several promotion routines of data governance
Thinking and summary of technical ability
Design and practice of low code real-time data warehouse construction system
Comparison between Prometheus and ZABBIX
3.pyinstaller module introduction
China micronutrient market trend report, technical innovation and market forecast