Cocos2d-Lua02Lua object-oriented
1 object-oriented 1.1 in Lua
Lua is not an object-oriented language, but he has an object-oriented idea. In lua, the object orientation is pseudo-object-oriented, and the object orientation must be table-based.
A table simulates an object-oriented programming. A table can also be viewed as an object. Like other language objects, a table has its own identifier (self) and has its own lifecycle. This case demonstrates object-oriented in lua.
1.2 Solution
First, use table to implement object-oriented.
Then, use self to obtain the current lable object.
Next, use the symbol to implement the implicit test parameter self when declaring the function.
Finally, return an object through require and call this object method.
Step 1
To implement this case, follow these steps.
Step 1: Use table to implement object-oriented
First, define a table-type data Account, then declare the label object, and call the method through the object. The Code is as follows:
- Accout = {balance = 0}
- FunctionAccout. count (v)
- Print ("value is" .. v)
- End
- -- Declare the label object (variable) accout
- Accout = Accout
- -- Call methods through objects
- Accout. count (1000)
The effect in the editor is shown in-13:
Figure 13
Step 2: Use self to obtain the current lable object
Through the object call method, the displayed object "accout" is passed into the function (display incoming self). You can also use the colon call method to implicitly import the self Object Code as follows:
- Accout = {balance = 10}
- -- Display incoming self
- FunctionAccout. count (self, v)
- Self. balance = self. balance + v;
- Print ("value is"... self. balance)
- End
- -- Declare the label object (variable) accout
- Accout = Accout
- -- Pass the table accout to the function (display incoming self) through the object call method)
- Accout. count (accout, 1000)
- -- The self object can also be passed in through the: Call method.
- Accout: count (2000)
The effect in the editor is shown in-14:
Figure 14
Step 3: Use a colon to declare implicit parameters of a function
You can use the colon symbol to implicitly pass in the self object. The Code is as follows:
- Accout = {balance = 10}
- -- Pass self through: Symbol
- FunctionAccout: count (v)
- Self. balance = self. balance + v;
- Print ("value is"... self. balance)
- End
-
- -- Declare the label object (variable) accout
- Accout = Accout
- -- The self object can also be passed in through the: Call method.
- Accout: count (2000)
The effect in the editor is shown in-15:
Figure-15
Step 4: return objects through require
Use require to return an object and call this object method. The Code is as follows:
- Class = require ("myClass ")
- -- The function is written outside the brackets of the table.
- Class: showName ()
- -- The function is written in brackets of the table.
- Class. show (class)
The effect in the editor is shown in-16:
Figure 16
------------- "Require (" XXX ") -- only executes once, avoiding repeated execution
1.4 complete code
In this case, the complete code is as follows:
- -- [[Lua is not an object-oriented language, but he has an object-oriented idea.
- In lua, the object orientation is pseudo-object-oriented, and the object orientation must be table-based,
- It is table that simulates an object-oriented programming. table can also be seen as an object,
- Similar to other language objects, a self has its own lifecycle.]
-
- -- 1. Use table to implement object-oriented
- Accout = {balance = 0}
-
- FunctionAccout. count (v)
- Print ("value is" .. v)
- End
-
- -- Declare the label object (variable) accout
- Accout = Accout
- -- Call methods through objects
- Accout. count (1000)
-
-
-
- -- 2. Get the current lable object through self
- Accout = {balance = 10}
- -- Display incoming self
- FunctionAccout. count (self, v)
- Self. balance = self. balance + v;
- Print ("value is"... self. balance)
- End
-
- -- Declare the label object (variable) accout
- Accout = Accout
- -- Pass the table accout to the function (display incoming self) through the object call method)
- Accout. count (accout, 1000)
- -- The self object can also be passed in through the: Call method.
- Accout: count (2000)
-
-
- -- 3. Use the symbol to implement the implicit test parameter self when declaring the function.
- Accout = {balance = 10}
- -- Pass self through: Symbol
- FunctionAccout: count (v)
- Self. balance = self. balance + v;
- Print ("value is"... self. balance)
- End
-
- -- Declare the label object (variable) accout
- Accout = Accout
- -- The self object can also be passed in through the: Call method.
- Accout: count (2000)
-
-
- -- 4. Return an object through require and call this object Method
- Class = require ("myClass ")
- -- The function is written outside the brackets of the table.
- Class: showName ()
- -- The function is written in brackets of the table.
- Class. show (class) 2. metadatabase method and metadatabase method: a function that operates between two types of variables, for example, 1. the addition of a number may be just a function: 1 + 1 ---> add () ---> the add function is used to calculate the result of adding two numbers to the de2.table type? ---> There are no two metamethods in Lua: {} +! Metadata table: A table used to store metadata. It is a series of operations predefined by a table. After adding two tables, Lua will first check whether the two tables have retriable, and then check whether the _ add method exists in the retriable. If any operation is performed in the _ add method, otherwise, an error is returned. For example, if you want to add two table types, an error is reported when running the command directly. However, Lua allows you to modify the metadata table as follows:
A metadatabase is actually a table value. Therefore, we only need to create a new table and add the metadatabase method.
For example, the meta method of the addition operation is __add, which is defined by Lua.
As long as the meta table of a value contains the _ add meta method, you can use the + number for calculation.
The following code:
- -- Create a metadata table
- Local mt = {};
- Mt. _ add = function (t1, t2)
- Print ("the result of adding two tables is... Good Study, Day Up! ");
- End
-
- Local t1 = {};
- Local t2 = {};
-
- -- Set a new metadata table for the two tables
- Setretriable (t1, mt );
- Setretriable (t2, mt );
-
- -- Perform Addition
- Local result = t1 + t2;
First, create a table variable mt and add an element _ add to the table. This table is qualified as a table.
Create two new table variables and use the setretriable function to set a new table metadata. At this time, the two table variables use mt as the metadata table.
Finally, the addition operation is performed on t1 and t2, And the _ add meta method is searched from the meta table. If yes, the meta method is called to add the two variables.
Each value in Lua has or can have a metadata table, but table and userdata can have independent metadata tables. Values of other types can only share the elements of their types. For example, a string uses a string meta table. Note: When creating a table, Lua does not create a readable table. You must use setretriable to set the metadata table. The setretriable parameter allows any table, including the table to be assigned a value. ------ Check whether a variable has a metadata table: print (getretriable (a) -- nil indicates that there is no important metadata method in the metadata table Lua: __eq is equal to, _ lt is less than, _ le is less than or equal to, _ add: addition, _ sub: subtraction, _ mul: Multiplication, _ div: division, _ unm: inverse number, _ mod: modulo, _ pow: Power Multiplication, _ index function (table, key), _ newindex function (table, key, value), _ tostring is called by print (), and _ resumable can be used to hide mt.
You only need to assign new functions to these functions when customizing the metadata table.
Lua accesses the table element. First, it uses the _ index meta method to find whether this function exists (if not, nil is returned); __ the index value can be a table directly, it can also be a function (if it is a table, the table is used as an index for query; if it is a function, the table and the missing fields are used as parameters to call this function)
Example 1,
Create a custom meta table (that is, a table variable) to define some operations:
- -- Create a metadata table
- Local mt = {};
- Mt. _ add = function (s1, s2)
- Local result = "";
- If s1.sex = "boy" and s2.sex = "girl" then
- Result = "qingmei Bamboo ";
- Elseif s1.sex = "girl" and s2.sex = "girl" then
- Result = "empty or empty ";
- Else
- Result = "hard-hitting"
- End
-
- Return result;
- End
In fact, this is basically the same as the example in the previous section. I just want to talk about it once. The usage is as follows:
- -- Create two tables, which can be imagined as objects of two classes
- Local s1 = {
- Name = "Hello ",
- Sex = "boy ",
- };
- Local s2 = {
- Name = "Good ",
- Sex = "girl ",
- };
-
- -- Set a new metadata table for the two tables
- Setretriable (s1, mt );
- Setretriable (s2, mt );
-
- -- Perform Addition
- Local result = s1 + s2;
-
- Print (result); example 2. Window = {} Window. mt = {} Window. prototype = {x = 0, y = 0, width = 100, height = 100} Window. mt. _ index = function (table, key) return Window. prototype [key] end function Window. new (t) setretriable (t, Window. mt) return tend -- Test w = Window. new {x = 10, y = 20} print (w. the usage of heigth) _ newindex is basically the same as that of Example 2. The difference is that _ newindex is used for table update, __index is used for table query operations. When a value is assigned to an index that does not exist in the table, the _ newindex meta method is called. The combination of _ index and _ newindex can implement a lot of functional encapsulation: With the metadata table, the Lua class encapsulation will become very easy. You only need to add a retriable to the table and set the _ index method. For example: People = {age = 18} function People: new () local p = {} setretriable (p, self) self. _ index = selfreturn pend function People: groupUp () self. age = self. age + 1 print (self. age) end -- Test p1 = People: new () p1: growUp () -- 19p2 = People: new () p2: growUp () -- 19 -- [[the running results show that: the age members of the two objects are completely independent, and all the People-related methods can be invisible to the outside. In this way, the class encapsulation in the object-oriented model is fully realized --] inheritance: inheritance is an essential part of object-oriented systems. We still use People in the above example to demonstrate the methods for implementing inheritance in Lua. Create a People instance Man, and then override the People method with the same name on Man: Man = People: new () function Man: growUp () self. age = self. age + 1 print ("man's growUp :".. self. age) end -- Test man1 = Man: new () man1.growUp () -- man's growUp: 19 polymorphism: Lua does not support function polymorphism, but supports pointer polymorphism, lua is supported by its dynamic type. Person = People: new () person: growUp () -- People's growUp: 19 person = Man: new () person: growUp () -- Man's growUp: 19