COCOS2D-X Lua Object-Oriented Programming

Source: Internet
Author: User

Monkey original, reprinted. Reprinted Please note: Reprinted from cocos2d Development Network-cocos2dev.com, thank you!

Original address: http://www.cocos2dev.com /? P = 425

A classmate asked me last time that Lua is too simple and has no structure, and it is difficult to do object-oriented programming. In fact, the table in Lua is an object.

Next I will introduce Lua's object-oriented programming.

I. Object method functions:

  Hero = {attack = 100}    function Hero.skill(addAttack)    Hero.attack = Hero.attack + addAttack  end    Hero.skill(20)  print(Hero.attack)--> 120

First, a function is created and called.

You may find that the global hero is used to call the function. I mentioned in the previous article that Lua should minimize the use of global variables. There are also risks. If hero is accidentally modified, hero may not work properly.

Some people may think of modifying the above function call like this when we think of the local TEMA = a written in the previous article:

Onehero = hero; hero = nil onehero. Skill (30) print (onehero. Attack) --> error, onehero is nil

This is also wrong. Because hero is already nil.

How can I modify the correct one? We can use this/self to implement:

  Hero = {attack = 100}    function Hero.skill(self,addAttack)    self.attack = self.attack + addAttack  end    oneHero = Hero;   Hero = nil  oneHero.skill(oneHero,30)  print(oneHero.attack) --> 130

OK, this is okay, but each time self needs to be passed by itself, it will be troublesome to look at it. In fact, Lua can also implicitly call self. Let's modify it:

  Hero = {attack = 100}    function Hero:skill(addAttack)    self.attack = self.attack + addAttack  end    oneHero = Hero;   Hero = nil  oneHero:skill(30)  print(oneHero.attack) --> 130

Note the preceding usage: You can add an additional hidden parameter to the method using the colon. The difference between hero. Skill () and hero: Skill () is also seen above.

Ii. Class: Use table as its own meta-table
For example, in the hero example above, we modify the following:

  Hero = {}    function Hero:new(o)    o = o or {}    setmetatable(o,self)    self.__index = self    return o  end    function Hero:skill(addAttack)    self.attack = self.attack + addAttack  end    oneHero = Hero:new{attack = 90}  oneHero:skill(10)  print(oneHero.attack) -->100

When creating a new hero, onehero sets hero as its own meta table. When onehero: Skill (10), search for skill in Table onehero, the _ index of the Meta table is further searched.

So it is equivalent to: getretriable (onehero). _ index. Skill (onehero, 10)

While getcyclable (onehero) is hero, hero. _ index or hero

So it is equivalent to hero. Skill (onehero, 10)

Iii. Inheritance
For example:

 Hero = {attack = 0}    function Hero:new(o)    o = o or {}    setmetatable(o,self)    self.__index = self    return o  end    function Hero:skill(addAttack)    self.attack = self.attack + addAttack  end    function Hero:injured(loseAttack)    if loseAttack > self.attack then error"not engouth attack" end    self.attack = self.attack - loseAttack  end    HumanHero = Hero:new()    oneHero = HumanHero:new{attack = 100}  oneHero:skill(10)    print(oneHero.attack) -->110

Methods not available to each object are searched in the parent class (this is understood as the parent class). Therefore, if an object requires some new attribute methods, you only need to implement them in the object.

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.