LUA表与函数的深入理解
生活随笔
收集整理的這篇文章主要介紹了
LUA表与函数的深入理解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
LUA表與函數(shù)的深入理解
local heroInfo = {}--直接打印 table的名字,就會(huì)輸出該table的內(nèi)存地址 print("表地址---------------",heroInfo)--注意區(qū)別PrintInfo,PrintInfo2與TestSelf兩個(gè)函數(shù)中self的不同 --有什么不? heroInfo.PrintInfo = function(self, x)--這里的self僅是我們定義的普通參數(shù),可以寫成任何樣子print("PrintInfo-------------x=", x)self.gender = "men"print("gender=", heroInfo.gender) --輸出 gender=men,為什么? end--定義函數(shù)時(shí)使用點(diǎn)號(hào),self并不存在 heroInfo.PrintInfo2 = function(x)print("PrintInfo2-------------self=", self, ",x=", x) end--定義函數(shù)時(shí)使用冒號(hào),系統(tǒng)自動(dòng)把表地址賦值給了self function heroInfo:TestSelf(x)--注意,參數(shù)并沒有寫selfprint("TestSelf--------------self=", self, ",x=", x)self.name = "jim"print("heroInfo.name=",heroInfo.name)heroInfo.name = "none"print("self.name=", self.name) end--函數(shù)調(diào)用 --使用冒號(hào)調(diào)用時(shí),系統(tǒng)會(huì)自動(dòng)把表地址傳給函數(shù)的第一個(gè)參數(shù),使用點(diǎn)號(hào)則不會(huì) --這個(gè)參數(shù)可以寫成self,也可以寫成其它任何樣子 --這類似于C++中的this指針 heroInfo:PrintInfo("hello")--改成點(diǎn)號(hào)調(diào)用測(cè)試下 heroInfo:PrintInfo2("hello", "world")--改成點(diǎn)號(hào)調(diào)用測(cè)試下 heroInfo:TestSelf(10)--改成點(diǎn)號(hào)調(diào)用測(cè)試下--lua中table是一個(gè)引用型變量,就像C#中的數(shù)組,類對(duì)象 local self = heroInfo --從此以后ref與heroInfo就指向同一個(gè)表了print("self.name=",self.name, "self.gender=",self.gender) self.name = "monster" self.gender = "unknown" print("heroInfo.name=", heroInfo.name, "heroInfo.gender=", heroInfo.gender)?
posted on 2017-02-24 10:42 時(shí)空觀察者9號(hào) 閱讀(...) 評(píng)論(...) 編輯 收藏
總結(jié)
以上是生活随笔為你收集整理的LUA表与函数的深入理解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LUA 删除元素的问题
- 下一篇: Resources与StreamingA