当前位置:网站首页>Closure problem C Lua

Closure problem C Lua

2022-06-26 06:32:00 ThomasQxx

Closure problem summary ( There was a big guy who asked a few questions from time to time , Now let's sum up the experience )

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}");
    }
}

Here you can guess and choose Toggle What will the print result be ?Toggle There are three radio buttons .

You can see Index Always be 3...

Why would the result be 3 Well ? The concept of closure will be mentioned here : The inner function can refer to the variables of the function contained in its outer layer , Even if the execution of the outer function has terminated . However, the value provided by this variable is not the value at the time of variable creation , But the final value in the range of the parent function . Normally speaking For When the loop is finished i The memory of variables will change with For The cycle ends and life ends . But because anonymous functions refer to i, Lead to i Can't go with For The end of the cycle . So at this point i=3. When we execute a click index=3. Here is the problem , So how to solve it ?

Solution

 Insert picture description here

That's it . Want to figure out the solution , We still have to start with memory . The last one did not int t = i In the code of .i There are several memory addresses ? You can see it i Only one memory address . In the current figure t There are several memory addresses ? There are obviously three , Every time For Loop in will declare a t Memory address of . also t The value of the For The end of the loop is different . Namely 0,1,2. So the problem can be fully understood here .

 Insert picture description here

Lua Closure in

function newCounter()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end
c1 = newCounter()
print(c1())
print(c1())

The result should be 1,2.i It should have been NewCounter At the end of the call, the memory is released . But because of the internal function reference , So memory continues to exist , First execution completed c1()i = 0+1 = 1; Second execution c1() End i It should be for 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())

The result should be 1,1. Because after the first execution c1()i = i + 1 = 0 + 1 = 1; But next c1=nil It frees up memory .c2 = newCounter() perform c2() At this time i by 0. therefore c2() The result is i = i + 1 = 0 + 1 = 1; So the end result is 1,1.

原网站

版权声明
本文为[ThomasQxx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260621105593.html