Lua object-oriented multiple inheritance, privacy detailed _lua

Source: Internet
Author: User
Tags inheritance lua one table

Multiple inheritance and privacy may be used less in Lua, or it may just be that I am using less personally.
Originally wanted to be lazy not to write this article, because I just bought a drift board today, even started have not learned Ah, want to learn a little more.

Cough, in the spirit of perseverance, responsible for the end of the attitude, I still decided to write a few words ~ (Jor: Casually write a few words is a few tons of meaning ah?!) )

1. Multiple inheritance find a field in multiple classes

I find these high (shen) (jing) (Bing) People are really strong, this technique can be thought of, very admire.
In fact, multiple inheritance is nothing special, unless the two classes that will be inherited have the same function name and attributes, otherwise it is easy to handle.

It's just that finding a field in multiple table is not easy? The continuation of LUA is to find a field in someone else's table that doesn't exist.

So the difference between single inheritance and multiple inheritance is here, one is to look for only one table, and the other is to find two or more table.

Let's take a look at how to find a field from 2 or more table, the following code:

Copy Code code as follows:

function search (classes, key)
For i = 1, #classes do
Local value = Classes[i][key];
If value ~= nil then
return value;
End
End
End
Local T1 = {name = ' hehe '};
Local T2 = {game = ' who '};
Print (search ({t1, t2}, "game"));

The classes argument here is a table with more than one table in it, which is the class we want to inherit.

And key is the field to find.

Just go through all the table, judge whether the field is in a table, and then return the value after it is found.
Our test code is to find game this field from T1, T2, T1, T1 can be considered as two classes.
The output results are as follows:

Copy Code code as follows:

[Lua-print] Who

2. Multiple inheritance creates subclasses that inherit multiple classes

Just the search function is simple? Don't be so happy, it's just a warm-up. The functions that actually create multiple inheritance are more complex.

The following code:

Copy Code code as follows:

function Createclass (...)
Local parents = {...};
Local child = {};

--Set the Wenzu of the class
Setmetatable (Child, {
__index = function (table, key)
Return search (parents, key);
End
})

--Add a new function to the class to create the object
function Child:new ()
o = {};
Setmetatable (o, child);
Child.__index = child;
return o;
End

--Returns the subclass that inherits more than one class
return to child;
End

The Createclass function is used to create a subclass that inherits multiple classes, a little complicated, and slowly analyzes:

1 parameter is a variable parameter, we want to pass multiple inherited classes as parameters in
2) Parents is used to save these inherited classes
3 Create a new table--child, it's the one we want. Subclasses inheriting multiple classes
4) Set the meta table for the child, and set the __index method, the __index method can be a function, when it is a function, its argument is the table to which the metadirectory belongs, and the name of the field to find.
5 We call the search function in the __index meta method function to find the desired field from multiple parent classes. As a result, when a function of the child is invoked, it is looked up from each parent class, which completes the inherited work.
6 Next is the new function that we are familiar with to create subclasses of the child, implemented in the same way as in the previous article, if you forget, you can read this article: http://www.jb51.net/article/55168.htm
7 finally returned the child and everything was done.

Seemingly very complex, in fact, is still the application of __index.

Let's take a quick test, the following code:

Copy Code code as follows:

--An Elf class
Tsprite = {}
function Tsprite:hello ()
Print ("Who hello! with you? ");
End

function Tsprite:new ()
o = {}
Setmetatable (o, self);
Self.__index = self;
return o;
End

--A bullet type
Tbullet = {}
function Tbullet:fire ()
Print ("Don't move, move again!") ");
End
function Tbullet:new ()
o = {}
Setmetatable (o, self);
Self.__index = self;
return o;
End

--inheriting subclasses of two classes
Local bulletsprite = Createclass (Tsprite, Tbullet);

--Object of subclass
Local bsprite = Bulletsprite:new ();
Bsprite:hello ();
Bsprite:fire ();

Two classes are created here: Tsprite and Tbullet.
Then call the Createclass function to create a subclass that inherits the Tsprite and Tbullet.
Finally, the object of the subclass is created, and the Hello and fire functions of the object are called.
The output results are as follows:

Copy Code code as follows:

[Lua-print] who hello! with you
[Lua-print] Don't move, I can't aim!

What do you think? It's simple.

3. Privacy of Class

One technique that has nothing to do with multiple inheritance is privacy.

For Java, C + + and other languages, we are very familiar with, public, private, protected and other key words.
These keywords make encapsulation possible.

Then there is no privacy in Lua, and all the fields of a class and a table,table are callable, without saying what is public and what is private.

If you have certain functions and properties that you do not want to be called externally, you can, but this implementation looks awkward:

Copy Code code as follows:

function Createtsprite ()
Local Self = {name = "Benmutou"};
Local function MyBus ()
Print ("MyBus is my own function, you cannot call directly");
End
Local function Mygame ()
Print ("Mygame is my own function, you cannot call directly");
End
Local function Hello ()
Print ("Hello:");
MyBus ();
End
Local function Hi ()
Print ("Hi:");
Mygame ();
End

Local function SetName (newName)
Self.name = NewName;
End

return {hello = hello, hi = Hi, setname = setname};
End

We don't have to use a colon to define a function, and the name, MyBus, and mygame of this class do not want to be called externally directly.

When the Createtsprite function is called, a new table is returned, which only holds a few fields, which are functions that can be called directly externally or attributes.

Take a look at the test code:

Copy Code code as follows:

Local SP = Createtsprite ();
Sp.hello ();
Sp.hi ();

The output results are as follows:

Copy Code code as follows:

[Lua-print] Hello:
[Lua-print] MyBus is my own function, you can't call directly
[Lua-print] Hi:
[Lua-print] mygame is my own function, you can't call directly

In this way, the objects we create can only use the Hello, Hi, setname functions.

Other name, MyBus, and mygame can only be invoked through these functions that can be used, not directly.
This will be done with privacy.

But I'm a bit confused because it's not like a class anymore.

4. End

OK, about object-oriented content, temporarily introduced here.
All of the possible comparisons are based on the goal of consolidating the LUA Foundation.

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.