On the implementation principle of classes in Lua (methods for implementing classes in Lua) _lua

Source: Internet
Author: User
Tags lua

There is no concept of a class in Lua, but we can use the language features of Lua itself to implement classes.

The following detail explains the principles of implementing classes in Lua, the details to be split, and the students who believe that the understanding of the implementation classes in Lua is difficult will be explained.

What is a class?

To implement a class, you need to know what the class is.

In my opinion, a class is a variable type that is defined by itself. It has agreed on some of its properties and methods, and is a collection of properties and methods.

All methods require a name, even if the anonymous function actually has a name. This forms the key-value mapping relationship between the method name and the method function, that is, the method name is the key and the mapped value is the method function.

For example, a class is a person, the person has a method of speaking, that is equivalent to, the person is a class, speak (talk) is its method name, the speech function is its actual speech to carry out the content.

People also have a property, such as gender, gender is a key (sex), the actual value of the gender is the corresponding content of this key.

Understanding that a class is actually a set of key-value pairs, it is not difficult to think of a class with a table in Lua.

What is the instance?

If you understand that a class is actually a table of key-value mappings, then let's understand what the instance is.

An instance is a collection of properties and methods that have classes, and a table. Sounds like a class to me?

Class global only one set, the equivalent of God, the global only a piece of memory, and the instance is ordinary, there are so many people in the whole, you can call a said a word, a will carry out his way of speaking, but will not affect the speech of B. Because they are instances, different memory is allocated to each other.

So much nonsense, in fact, the example is the value created by the class, try to think of the class as a type rather than a class.

Two grammatical sugars

Try to create a human person.

Copy Code code as follows:

person = {Name= "This man is Lazy"}

The above code initializes a person to a table that has a key named name, and the default value is "This guy is lazy".

The vernacular is that humans have a property called name.

Then give the human a function of speaking.

Copy Code code as follows:

Person.talk = function (self, words)
Print (Self.name ...) said: ". Words
End

The above code adds a key value pair to the person table, the key is talk, and the value is a function.

Well, as soon as the call, Person.talk (person, "hello"), will print out: "This man is lazy to say:" Hello.

But when it comes to writing programs, people are used to putting functions in front of them, which is the syntactic candy of the function:

Copy Code code as follows:

function Person.talk (self, words)
Print (Self.name ...) said: ". Words
End

This is equivalent to the definition of the function above, but it's hard to see that talk is actually a key in the person table, and its corresponding value is a function.

Of course, the mouth is long on their own body, talk can only own said, it is impossible to open their mouths to speak, so each time to pass a self parameter is a bit ugly, so the colon grammar sugar field.

We can also define human speech functions in this way:

Copy Code code as follows:

function Person:talk (words)
Print (Self.name ...) said: ". Words
End

This is equivalent to the above two pieces of code, and it changes less than the self parameter, changing the point person.talk to a colon person:talk.

But in a function body, you can still use self, in use: instead of., the first argument of the function's argument list is no longer words,lua automatically takes self as the first argument. This self parameter represents the actual caller of this function.

So we call Person:talk ("Hello") and Person.talk (person, "hello") is equivalent, which is the convenience of the colon syntax sugar.

How do I find elements in a table?

Below we need to understand how to find the value of a key in the LUA table.

Let's say we want to find the value of the Talk key in table p, see the flowchart below:

Copy Code code as follows:

Is there talk this key in P? Have--> return talk corresponding value
|
No
|
is metatable set in P? No--> return nil
|
Yes
|
Is there a __index key in P's metatable? No--> return nil
|
Yes
|
Is there a talk in the table in P's metatable that corresponds to __index this key? No--> return nil
|
There, return Getmetatable (p). __index.talk

Understanding the above content is the focus of this article, read it again and again until you remember.

As you can see, because of the magic of MetaTable and __index, LUA can find its return value when the key does not exist in the current table.

The following is a talk about the language features of metatable.

The understanding of MetaTable

What is MetaTable?

MetaTable's Chinese name is a meta table. It's not a separate type, the meta table is actually a table.

We know that tables are limited in Lua, for example, tables cannot be added directly, comparisons cannot be done, and so on.

The role of Wenzu is to increase and alter the established operation of the table. Only the table that sets the meta table will be affected by the meta table to change its behavior.

The global method Setmetatable (T, m) sets the meta table for table T to table M. Getmetatable (t) through another global method returns its meta table M.

Note: All tables can set the meta table, however, if the newly created empty table is not set, there is no meta table.

Meta method

Wenzu as a table, you can have any type of key-value pairs, and its real effect on the table being set is the LUA-defined meta-method key-value pair.

These key-value pairs are the keys required by LUA, such as the __index,__add,__concat mentioned earlier. These key names are prefixed with a double slash __. The corresponding value is a function, called the Meta Method (Metamethod), which defines the actions you want to customize to the table.

For example, the __index key, which is mentioned earlier, is executed by the Meta method in Lua when looking for a key that does not exist in the table. Consider the following code:

Copy Code code as follows:

--Define meta table M
m = {}
--a meta method for defining the __index of the meta table
--Return "undefined" to any key that cannot be found
M.__index = function (table, key)
return "undefined"
End

--Table Pos
pos = {x=1, y=2}
--There is no meta table initially, so there is no definition of the behavior that cannot be found
--Because z is not in Pos, return directly to nil
Print (POS.Z)--Nil
--Set the POS Wenzu to M
Setmetatable (POS, M)
This is although the Pos still can't find Z, but because the POS has a meta table,
--and the meta table has a __index property, so execute its corresponding meta method, and return to "undefined"
Print (pos.z)--undefined

There is no Z this key in the POS table, by setting the Pos Wenzu to M and setting the __index corresponding method of M, so that all keys that are not taken will return "undefined".

We have learned that the __index property of the meta table is actually a table with the behavior of not finding the key.

Note: The __index attribute of the meta table can also be a table.

Another chestnut, hoping to deepen the understanding of the meta table and the Meta method, __add key, consider the following code:

Copy Code code as follows:

--Create a meta table M with the __add key and the method it defines
Local m = {
__add = function (t1, T2)
Local sum = {}
For key, value in pairs (T1) do
Sum[key] = value
End

For key, value in pairs (T2) do
If Sum[key] Then
Sum[key] = Sum[key] + value
Else
Sum[key] = value
End
End
return sum
End
}

--Set both Table1 and table2 to M
Local table1 = setmetatable ({Ten, One,}, M)
Local table2 = setmetatable ({, n}, M)

--The table was not able to perform the + operation, but through the meta table, we did!
For K, v. in pairs (table1 + table2) do
Print (k, v)
End
--print
--1 23
--2 25
--3 27

The table itself cannot be calculated with +, but by defining the __add of the meta table and setmetatable to the table where the action is desired, those tables can be added.

Because the __add property of the meta table is the behavior that defines the use of the + number for the table.

The implementation of the class method

OK, let's say you don't have any questions before you read the words, we start to get to the point.

Just think for a minute, how do we implement a LUA class?

Thinking about ing ...

After all sorts of matting, our class is a table that defines a variety of properties and methods. Our example is also a table, and then our class is set to the instance as a meta table, and the __index value of the class is set to itself.

such as human beings:

Copy Code code as follows:

--Set the person's __index for itself
Person.__index = person

--p is an example
Local p = {}

--p's meta table set to Person
Setmetatable (p, person)

P.name = "Passerby"

--p would have been an empty table, without talk this key
--but P has a meta table, and the __index property of the meta table is a table person
-and the person has talk this key, and then executes the person's talk function
--Default parameter self is the Name property of caller P,p as "passerby"
P:talk ("I'm a Passerby")

--so get the output
"I am a passerby," said Passerby

For convenience, we give the human a creation function create:

Copy Code code as follows:

function Person:create (name)
Local p = {}
Setmetatable (p, person)
P.name = Name
Return P
End

Local PA = person:create ("Passerby a")
Local PB = Person:create ("Passerby B")
Pa:talk ("I am a Passerby")--passerby Jia said: I am a passerby
Pb:talk ("I'm Passerby B")---------Passerby B said: "I am passerby b

This makes it easy to create the PA and PB two instances with the person class, which all have the properties and method of person.

This is the way Lua implements a class, as for class inheritance, as an exercise, please think about it ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.