[Go] object-oriented Implementation Method in Lua

Source: Internet
Author: User

 

Object-Oriented programming is not an idea for a language. In a process-oriented language, you can use object-oriented programming. In Lua, there is no object-oriented concept, and there is no class definition or subclass definition. However, in Lua, the idea of object-oriented can also be used to implement object-oriented class inheritance.

 

I. Copying tables is object-oriented.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 -- Object-oriented in Lua--[[  Table Copying Method object-oriented  The 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 clone(tab)    local ins = {}    for key, var in pairs(tab) do        ins[key] = var    end    return insend --[[  Copy a table  The first parameter is the target table, and the second parameter is the table to be copied.  Assign a value to the target table by traversing the tab.]]function copy(dist,tab)    for key, var in pairs(tab) do        dist[key] = var    endend -- Define an empty table, which is equivalent to a classPeople ={} --function People.sayHi()--  print("People say hi")--end -- Define the method sayhi in the class and input a self ParameterPeople.sayHi = function (self)    print("People say hi:"..self.name)end -- DefinenewTo generate an object by cloning the people table.-- It is equivalent to a constructor in the class.People.new = function (name)    local self = clone(People)    self.name = name         return selfend --local p = clone(People)--p.sayHi()-- Generate a new objectlocal p = People.new("ZhangSan")--p.sayHi(p)--p:sayHi()-- Define an empty table, which also represents a classMan = {}-- Man constructor to implement class inheritance in LuaMan.new = function (name)    local self = People.new(name)    -- Append all the key-value pairs in man to the instance in people.    copy(self,Man)    return selfend Man.sayHello = function ()    print("Man say hello")end -- Override the sayhi method in the parent class peopleMan.sayHi = function (self)    print("Man sayHi "..self.name)end -- Create an instance of manlocal m = Man.new("Lisi")m:sayHi()

The above code has been annotated in detail, and readers need to carefully read the example given here.

 

2. Implement object-oriented functions in the form of function closures

123456789101112131415161718192021222324252627282930313233343536373839404142 -- 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 selfend -- Instantiate an objectlocal p = People("ZhangSan")p:sayHi() -- Implement class inheritance in the form of function closuresfunction Man(name)    local self = People(name)     --  local function init()--      --  end     self.sayHello = function ()        print("Hi "..self.name)    end         return selfendlocal m = Man("Lisi")--m:sayHello()m:sayHi()

The above two methods can achieve object-oriented, the second method of visual testing is more concise, specific use according to personal preferences.

 

Http://cn.cocos2d-x.org/tutorial/show? Id = 1133

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.