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