美文网首页
9、lua中的self,点号,冒号的关系

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

作者: GameObjectLgy | 来源:发表于2020-11-02 23:18 被阅读0次

    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)
    

    相关文章

      网友评论

          本文标题:9、lua中的self,点号,冒号的关系

          本文链接:https://www.haomeiwen.com/subject/dxqkmktx.html