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)
网友评论