Development of Cocos2d-x Lua game: Implementation of Lua oo

Source: Internet
Author: User

tag: Use OS 2014 for blog HTTP color

Some object-oriented languages provide the concept of classes as templates for creating objects. In these languages. The object is a class instance, and Lua can use the class through table. -- Create the parent class animalanimal = {size, -- declare the animal size member variable, without assigning the value nilage, -- animal age color = "yellow", -- animal color sex, -- animal gender, which can be ?? New = function (self, object) object = object or {} -- determines whether the object is empty. Nil will create an empty tablesetretriable (object, self) -- set the base class of animal as a meta table, which is an inherited method self. _ Index = self -- same as return objectend, sleep = function (self, PRAM) -- Lua supports: operators, instead. operator and display selfprint (PRAM, "Want to sleep well") -- humans, especially programmers, remember to sleep, no Nuo no dieend, run = function (self, PRAM) print (PRAM, "Want to exercise well") -- humans, especially programmers, remember sports, no Nuo no dieend, setage = function (self, age) -- Inherit the OC set, get method, check whether the macro self can be written later. age = age -- two parameters print ("setage =", self. age) return self. ageend, getage = function (Self) print ("getage =", self. age) return self. ageend, getcolor = function (Self) print ("getcolor =", self. color) return self. colorend} Cat = Animal: New () -- creates an empty object Cat: Sleep ("cat") cat. age = 22cat: setage (0) Cat: getage () Cat: getcolor () Snake = Animal: New {age = 33} snake: Sleep ("snake") Snake: getage () function snake: Sleep (animal) -- subclass Rewriting Method, You can implement the print ("Snake has its own nest to sleep") endsnake: sleep ("snake ")


Csdn does not support Lua, but it looks uncomfortable. Just upload a picture. Sublimetext used




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.