lua传"值"和传"引用"

例子一

[java]  view plain  copy
  1. x = 1  
  2. y = x  
  3. y = 10  
  4. print(x)  
  5.   
  6. 输出:1  

例子二

[java]  view plain  copy
  1. <pre name="code" class="java">function change(x)  
  2.   x = 10  
  3. end  
  4.   
  5. y = 1  
  6. change(y)  
  7. print(y)  
  8.   
  9. 输出:1  

 
 

例子三

[java]  view plain  copy
  1. x = "test"  
  2. y = x  
  3. x = "show"  
  4. print(y)  
  5.   
  6. 输出:test  


例子四

[java]  view plain  copy
  1. x = {abc = "123",456}  
  2. y = x  
  3. x.abc = "xixi"  
  4. print(y.abc)  
  5.   
  6. 输出:xixi  


例子五

[java]  view plain  copy
  1. function show()  
  2.   print("show some thing")  
  3. end   
  4.   
  5. function move()  
  6.   print("move to")  
  7. end  
  8.   
  9. x = show  
  10. y = x  
  11. x = move  
  12. y()  
  13. x()  
  14.   
  15. 输出:show some thing  
  16.       move to  

例子六

[java]  view plain  copy
  1. x = {123,"test"}  
  2. y = x[1]  
  3. x[1] = 456  
  4. print(y)  
  5.   
  6. 输出:123  



从上面的例子可以看出来,只有例子四相当于:传引用. 例子一,二,三,五,六都是:传值


所以可以说明:只有table是传引用(相当于一个指针a将地址传给指针b,它们所指向的内容都是一样的)


转自 https://blog.csdn.net/qweewqpkn/article/details/46728589

猜你喜欢

转载自blog.csdn.net/qq_28098067/article/details/80090014