The role and usage of "." and ":" in Lua study notes

table of Contents

1. Blog introduction

2. Content

meaning:

":" declares the method and calls in the form of ":":

":" declares the method and calls in the form of ".":

"." declares the method and calls in the form of ".":

"." declares the method and calls in the form of ":":

3. Push

4. Conclusion


 

1. Blog introduction

 

This blog, as one of the Lua study notes, introduces the usage and different meanings of "." and ":" in methods. We know that we can use "." or ":" when declaring a method, and call the method at the same time At the time, you can also use "." and ":". These two forms may intersect. This blog will briefly introduce a few situations.


2. Content

 

meaning:

1. Use ":" to declare the method, then when receiving the parameter, it will receive a parameter self by default, and the receiving position is hidden

2. Use "." to declare the method, the first digit in the parameter will default to receive the self parameter, this parameter is not hidden

3. Use ":" to call the method. When passing parameters, a self will be passed by default, which is hidden and placed before all parameters

4. Use "." to call the method. When passing parameters, the first parameter will be passed as self by default, and this parameter will not be hidden

As shown below, the method testFunc1 and the method testFunc2 are equivalent, and the output results of the following calls are the same

local func = {}

function func:testFunc1(a,b,c)
	print(a,b,c)
end

function func.testFunc2(self,a,b,c)
	print(a,b,c)
end

func:testFunc1(1,2,3)
func.testFunc1(self,1,2,3)

 

":" declares the method and calls in the form of ":":

testFunc1 is a method defined by ":", so the actual received parameter format should be (self, a, b, c), and the self parameter is hidden

The call here is in the form of ":", so the actual parameters passed are (self,1,2,3), and the self parameter is hidden

So the output result self here is the func table itself, and abc is 1 2 3

local func = {}

function func:testFunc1(a,b,c)
	print(self)
	print(a,b,c)
end

func:testFunc1(1,2,3)

-----------------------------------输出
table: 00C19238
1	2	3

 

":" declares the method and calls in the form of ".":

testFunc1 is a method defined by ":", so the actual received parameter format should be (self, a, b, c), and the self parameter is hidden

The call here is in the form of ".", the parameters passed are (1,2,3), 1 will be passed as self

So here the output result self is 1, abc is 2, 3, nil

local func = {}

function func:testFunc1(a,b,c)
	print(self)
	print(a,b,c)
end

func.testFunc1(1,2,3)

-----------------------------------输出
1
2	3	nil

 

"." declares the method and calls in the form of ".":

testFunc1 is a method defined by ".", so the format of the received parameter should be (self, a, b, c), and the self parameter will not be hidden

The call here is in the form of ".", the passed parameters are (func,1,2,3), func is passed as self

So here the output result self is func table, abc is 123

local func = {}

function func.testFunc1(self,a,b,c)
	print(self)
	print(a,b,c)
end

func.testFunc1(func,1,2,3)

-----------------------------------输出
table: 00AB9350
1	2	3

 

"." declares the method and calls in the form of ":":

testFunc1 is a method defined by ".", so the format of the received parameter should be (self, a, b, c), and the self parameter will not be hidden

The call here is in the form of ":", the actual parameters passed are (self,1,2,3), and self is func itself

So here the output result self is func table, abc is 123

local func = {}

function func.testFunc1(self,a,b,c)
	print(self)
	print(a,b,c)
end

func:testFunc1(1,2,3)

-----------------------------------输出
table: 00BC92B0
1	2	3

3. Push

Github:https://github.com/KingSun5


4. Conclusion

        If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/105196932