Lua Class and Class inheritance implementation

Source: Internet
Author: User

Lua itself cannot directly implement Inheritance like C ++, but we can implement it using a omnipotent table.

Below I have summarized three methods of class and inheritance implementation

1. for the official practice, refer to programming in Lua for the implementation principle of table meta.

Object. Lua

Object = {class_id = 0} function object: New (o) O = O or {} setretriable (O, self) -- when the object o calls a member that does not exist, it will be searched in self, here, self refers to object self. _ Index = selfreturn oend

--- Below we create objects to test the following
Local O1 = Object: New ()
O1.class _ id = 11;
local o2 = Object:new()
o2.class_id = 22;
 

We have implemented a class using the meta table above, but this class has no behavior. Below we inherit the class above

Displayobject. Lua

Displayobject = Object: New () -- so far, displayobject is only an instance of the object. Note the following code: D = displayobject: New (width = 100, Height = 50) -- displayobject inherits the new method from the object. When new is executed, the self parameter points to displayobject. Therefore, D's retriable is displayobject, __index is also displayobject. In this way, d inherits displayobject, and the latter inherits object. --- One interesting aspect of object-oriented in Lua is that you do not need to create a new class to specify a new behavior.

2. Table copying

We also use the above object in another way.

-- Object-oriented in Lua -- [copying a table as an object-oriented parameter is a table. By traversing the value of this table, an empty table is assigned and the newly created table is returned, to clone the table] function clonetab (Tab) Local ins ={} for key, VAR in pairs (Tab) Do INS [Key] = var end return ins end

Object = {class_id = 1}

Function object. New ()
Local o = clonetab (object)
Return o
End


-- Use this class
Local p = object. New ()

Inheritance implementation

DisplayObject.lua
-- [[The first parameter of the table to be copied is the target table, and the second parameter is the table to be copied. Assign the value to the target table by traversing the tab] function copy (Dist, Tab) for key, VAR in pairs (Tab) Do Dist [Key] = var end displayobject ={} function displayobject. new () local SS = object. new () Copy (SS, displayobject) return ssend
Local p1 = displayobject. New ()
 

Third, use the function closure to implement object-oriented

-- Implement object-oriented functions in the form of function closures -- Define a method, function closures implement the concept of a class function people (name) Local Self ={} -- initialization method, private local function Init () self. name = Name end self. sayhi = function () print ("hello ".. self. name) end -- call initialization Init () return self end -- instantiate an object local p = people ("zhangsan") P: sayhi () -- function closure form implement class inheritance function man (name) Local Self = people (name) -- local function Init () -- end self. sayhello = function () print ("hi ".. self. name) end return self end local M = man ("Lisi") -- M: sayhello () M: sayhi ()

 

 

PS; on the inheritance class, cocos2d-x V3 provides a better and more convenient way to achieve. Use the class global method to create

For example

Require "object" gameobject = Class ("gameobject", function () return object: New () End) function gameobject: Create () return gameobject. New () End
-- The above implements an inheritance process.

In the source code of the Cocos engine, there is the extern. Lua file, which declares the class method.

 

Lua Class and Class inheritance implementation

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.