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