9、lua中的self,点号,冒号的关系

2020-11-02  本文已影响0人  GameObjectLgy

lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号调用。

点号定义、点号调用
girl = {money = 200}
function girl.goToMarket(girl ,someMoney)
    girl.money = girl.money - someMoney
end
girl.goToMarket(girl ,100)
print(girl.money)
冒号定义、冒号调用
boy = {money = 200}
function boy:goToMarket(someMoney)
    self.money = self.money - someMoney
end
boy:goToMarket(100)
print(boy.money)
点定义、冒号调用
boy = {money = 200}
function boy.goToMarket(self ,someMoney)
    self.money = self.money - someMoney
end
boy:goToMarket(100)
print(boy.money)
冒点定义、点号调用
girl = {money = 200}
function girl:goToMarket(someMoney)
    self.money = someMoney
end
girl.goToMarket(girl, 100)
print(girl.money)
上一篇下一篇

猜你喜欢

热点阅读