Let Lua object-oriented--lua and Android

Source: Internet
Author: User
Tags lua

Make Lua object-oriented

LUA does not natively support object-oriented features, but since Lua is a prototype-based (prototype) language, there are certain ways to implement object-oriented features, and there are many ways to implement it, summarizing my recent use of LUA to implement object-oriented features, The main can be divided into the following two different ways to achieve:

1, the use of metatable __index domain implementation, the implementation of the need to use the Lua language features can be achieved, mainly:

A, table B as a prototype method of Table A: Setmetatable (A, {__index = b});

b, the module mechanism implemented in lua5.1;

With these two techniques you can implement a basic class that implements the inherit and new methods of the class:

-------------------------------------------------------------------------FileName: "Obj.lua" local setmetatable = setmetatable module "obj" function inherit (self) return function (Newclass) setmetatable (Newclass, self) self._ _index = self return Newclass end End Function New (self, o) o = O or {} setmetatable (O, self) self.__index = self return o end------------------------------------------------------------------------now can define a New class which extends the previous ' obj ':--filename: "Myobj.lua" local obj = require "obj" Module ("MyObj", obj:in Herit ())--class ' myobj ' would ' inherit ' the Methods ' new ' and ' Inherit ' from Class ' obj '.

Advantages:

1, because the subclass of a lot of data and methods are shared with the parent class, the use of the parent class data and methods, only when the use of the parent class directly call methods and data, which can reduce the consumption of program memory, more important, the parent class in the run-time changes will affect the subclass;

2, take full advantage of the Lua language characteristics, the parent class method and data access is the parser to do, so the cost of efficiency is relatively small;

Disadvantages:

1, if the parent class has a reference to the time (such as table), it will appear in a subclass operation of this table will change the situation of other subclasses, create inconsistent data, so should try to avoid this kind of creation, if there is such a need, You need to do something special with the Inherit and new functions, simply by adding an init function to create a display of all such data.

2, because each take operation need to take in metatable, so, each time will add a layer of inheritance, add a function call overhead, although it is done by the parser, but if the level is more, there is still overhead;

2, the use of table copy of the way to achieve, the implementation of the use of LUA technology is:

A, using LUA to implement a table copy of the function;

b, the module mechanism implemented in lua5.1;

With these two techniques you can implement a basic class that implements the inherit and new methods of the class:

-------------------------------------------------------------------------FileName: "Obj.lua" local setmetatable = setmetatable module "obj" function inherit (self) return function (newclass) Newclass = Table.clone (self) return Newclass End End Function New (self, o) o = O or {} o = Table.clone (self) return o End-------------------   -----------------------------------------------------now can define a new class which extends the previous ' obj ': --filename: "Myobj.lua" local obj = require "obj" Module ("MyObj", Obj:inherit ())--class ' MyObj ' would "inherit" the Methods ' new ' and ' Inherit ' from class ' obj '.

Advantages:

1, the data in the parent class is all copied to the subclass, so there is no inconsistency of data;

2, all the function calls and data calls are directly called each instance, do not need to find in the parent class;

Disadvantages:

1, the copy of all the data, at the time of creation will be added a table copy process, affecting efficiency;

2, all the data and methods are in the creation of a copy of the time, will add a lot of memory consumption, and if the runtime changes the parent class, and can not change the subclass;

Summarize:

In combination of these two ways, from an object-oriented point of view, the first way more suitable for the implementation of object-oriented features, the second approach to object-oriented simulation is a bit more (disadvantage 2), but from the perspective of use, because in the access to data and method speed, the second approach has advantages, so, In the specific use of the time, you can use the flexibility to combine the two.

For example, for the client side, the class will not need to be modified after the start of the creation, and the subclass generally requires all the methods and data of the parent class, all of us can use the second way, and when the object instance is generated, the instance of the object will not normally call all the methods of the class. And when we run out of this instance, it will be destroyed, so we can use the first way here, combining the design can be:

-------------------------------------------------------------------------FileName: "Obj.lua" local setmetatable = setmetatable module "obj" function inherit (self) return function (newclass) Newclass = Table.clone (self) return Newclass End End Function New (self, o) o = O or {} setmetatable (O, self) self.__index = self return o en D------------------------------------------------------------------------now can define a new class which extend s the previous ' obj ':--filename: "Myobj.lua" local obj = require "obj" Module ("MyObj", Obj:inherit ())--class ' Myo BJ ' would ' inherit "the Methods ' new ' and ' Inherit ' from class ' obj '.

Let Lua object-oriented--lua and Android

Related Article

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.