Object-oriented programming of LUA

Source: Internet
Author: User

First we go to the class, a class is a mold to create objects, in fact, there is no concept of class in Lua, we are going to emulate the class. In Lua, to represent a class, you simply create a prototype that is dedicated to other objects. Our object in Lua is to use table, meta-table, and meta-methods, and now we'll look at how things work:

We now have two objects A and B, and now we want B as a prototype, just write the following code:

Setmetatable (A,{__index = b})

We've talked about __index before, and after we've written this code, when Lua does a no-action, it goes to B. In fact, we can also call the B-Thing object A's class; we want to create an object, we must have a prototype, this is the "class". As follows:

Local man = {num = 0}

Now that we have the prototype class, and then we use this prototype to create an "instance," first we write the man's new function:

function Man:new (o) o = O or {}--if O is nil, copy O to {} setmetatable (o,self) Self.__index = self return oendfunc tion Man:sayhi () self.num = self.num + 1 print ("Person already greeted": self.num) End

When we call man's new function, self is the equivalent of man. Then we'll use the man's new function to create an instance:

Local Tom = Man:new () tom:sayhi () Tom:sayhi ()

Output:

People who have greeted 1 people who have already greeted 2

Let's take a look at how the above code works, first using the man's new function to create a new instance object, and the man as the new instance object Tom's meta-table. When we call Tom:sayhi (), Lua goes to find out if Tom has a Sayhi field, and then searches its meta-table, and the result of the call is actually:

Getmetatable (Tom) __index (Sayhi (Tom))

And Tom's meta-table is Man,man's __index is also man, the above writing is actually:

Man.sayhi (Tom)

In fact, we are also involved in the inheritance (Sayhi function). Then let's explore the multiple inheritance of Lua:

We can set the subclass's metatable as the parent class and set the parent class's __index to itself to implement single inheritance. And many inheritance is the same reason, the difference is the single inheritance if the subclass does not have corresponding fields, then only in a parent class to look for this nonexistent field, and multi-inheritance is to be found in more than one parent class.

Since multiple inheritance needs to be found in multiple parent classes, we cannot directly point to __index as a single parent class, but instead should specify __index as a function in which to specify rules for searching for fields that do not exist. This allows for multiple inheritance. Let's take a look at the following example:

--  lookup field Klocal function search (K, pparentlist) in multiple parent classes     for i  = 1,  #pParentList  do        local v =  pParentList[i][k]        if v then             return v         end    endendfunction createclass (...)     local c = {} --  New Class     local parents  = {...} The     --  class searches its meta-table for methods     setmetatable (c, {__index =  function  (t, k)  return search (k, parents)  end})     --  Set C as the Meta-table for its instance     c.__index = c    --  create a new constructor for this new class      function c:new (o)         o = o or {}         setmetatable (o, self)         -- self.__index  = self  here does not have to set up, on the above has set the c.__index = c         return o    end    --  return a new Class (prototype)      return cend--  a simple class calocal ca = {}function ca:new (o)     o  = o or {}    setmetatable (o, {__index = self})      SELF.__INDEX = SELF    RETURN OENDFUNCTION CA: SetName (strName)     self.name = strNameend--  a simple class cblocal cb =  {}function cb:new (o)     o = o or {}     Setmetatable (o, self)  &Nbsp;  self.__index = self    return oendfunction cb:getname ()     return self.nameend--  Create a Class C, whose parent class is CA and cblocal c =  Createclass (CA, CB)--  use Class C to create an instance object local objectc = c:new{name =  "Jelly"}--   Set OBJECTC object A new name Objectc:setname ("Jellythink") Local newname = objectc:getname () print ( NewName)

Note:

    1. A class (prototype) is created using Createclass, and the CA and CB are set to the parent class (prototype) of this class (archetype), and in the class (prototype) that is created, the __index of the class is set to a search function. In this search function, look for fields that are not in the created class;

    2. In the new class created, there is a constructor new; This new is not the same as the new one in the previous single inheritance, very well understood;

    3. Call the new constructor to create an instance object that has a name field;

    4. Call Object:setname ("Jellythink") statement, set a new name, but do not have this field in OBJECTC? OK, go to the parent class, first go to the CA to find, all of a sudden found, and then called the Setname,setname in the self point is objectc; After setting, it is equivalent to modifying the name value of the OBJECTC field;

    5. Call Objectc:getname (), OBJECTC still does not have this field. Find, CA also did not, then go to find, in the CB found, call GetName, in GetName in the self point is OBJECTC. Therefore, the value of name in OBJECTC is returned in Objectc:getname, which is "Jellythink".

In fact, the Lua class is to create a table, then bind several methods to the table and bind several objects. Then, some operations of the meta-table and meta-methods are performed to program the surface object of Lua.


Object-oriented programming of LUA

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.