Latest web development tutorials

Lua Object Oriented

OOP (Object Oriented Programming, OOP) is a very popular computer programming architecture.

The following programming languages ​​support object-oriented programming:

  • C ++
  • Java
  • Objective-C
  • Smalltalk
  • C #
  • Ruby

Object-oriented features

  • 1) Package: refers to the ability of an entity to information, function and responses are put in a separate object properties.
  • 2) Inheritance: the inherited method allows not change the basis of the original program on its expansion, such that the original function can be preserved, and the new features also been expanded. This helps to reduce repetitive coding, to improve the efficiency of software development.
  • 3) polymorphism: the same operation applied to different objects may have different interpretations, resulting in different execution results. At run time, you can point to base class pointer to call derived class methods.
  • 4) Abstract: Abstract (Abstraction) is to simplify the complex reality of the way, it can find the most appropriate category is defined as a specific issue, and may be in the most appropriate level inheritance interpretation.

Lua Object Oriented

We know that the composition of the object properties and methods. LUA is the most basic structure table, need to use the table to describe the properties of the object.

lua The function can be used for representation. The class then the LUA table + function can be simulated.

As for the inheritance can be simulated by metetable (not recommended to simulate only the most basic objects enough most of the time).

Lua tables not only in a sense, is an object. Like an object, the table also have state (member variables); also with the value of the object independent of nature, in particular those with two objects of different values ​​(table) represent two different objects; an object at different times may be different values, but he has always been an object; the object is similar to a table created by what their life cycle, in which no relationship is created. Objects have their member functions, tables also have:

Account = {balance = 0}
function Account.withdraw (v)
    Account.balance = Account.balance - v
end

This definition creates a new function, and saved withdraw within Account object, we can call the following:

Account.withdraw(100.00)

A simple example

The following simple class contains three properties: area, length and breadth, printArea method for printing results:

-- Meta class
Rectangle = {area = 0, length = 0, breadth = 0}

-- 派生类的方法 new
function Rectangle:new (o,length,breadth)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  self.length = length or 0
  self.breadth = breadth or 0
  self.area = length*breadth;
  return o
end

-- 派生类的方法 printArea
function Rectangle:printArea ()
  print("矩形面积为 ",self.area)
end

Create Object

The object is to create a process instance bit of class to allocate memory. Each class has its own memory and share common data.

r = Rectangle:new(nil,10,20)

Access Properties

We can use a dot to access the class attribute (.):

print(r.length)

Access member functions

We can use a colon (:) to attribute access classes:

r:printArea()

Memory allocation when the object is initialized.

Complete example

Below we demonstrate Lua object-oriented complete example:

-- Meta class
Shape = {area = 0}

-- 基础类方法 new
function Shape:new (o,side)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  side = side or 0
  self.area = side*side;
  return o
end

-- 基础类方法 printArea
function Shape:printArea ()
  print("面积为 ",self.area)
end

-- 创建对象
myshape = Shape:new(nil,10)

myshape:printArea()

The above program, the output is:

面积为 	100

Lua inheritance

Inheritance refers to an object directly using the properties and methods of another object. Properties and methods can be used to extend the base class.

The following demonstrates a simple inheritance examples:

 -- Meta class
Shape = {area = 0}
-- 基础类方法 new
function Shape:new (o,side)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  side = side or 0
  self.area = side*side;
  return o
end
-- 基础类方法 printArea
function Shape:printArea ()
  print("面积为 ",self.area)
end

The next instance, Square Shape objects inherit the classes:

Square = Shape:new()
-- Derived class method new
function Square:new (o,side)
  o = o or Shape:new(o,side)
  setmetatable(o, self)
  self.__index = self
  return o
end

Complete example

The following examples we inherited a simple class method to extend the derived class, the derived class to retain the member variables and methods inherited classes:

 -- Meta class
Shape = {area = 0}
-- 基础类方法 new
function Shape:new (o,side)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  side = side or 0
  self.area = side*side;
  return o
end
-- 基础类方法 printArea
function Shape:printArea ()
  print("面积为 ",self.area)
end

-- 创建对象
myshape = Shape:new(nil,10)
myshape:printArea()

Square = Shape:new()
-- 派生类方法 new
function Square:new (o,side)
  o = o or Shape:new(o,side)
  setmetatable(o, self)
  self.__index = self
  return o
end

-- 派生类方法 printArea
function Square:printArea ()
  print("正方形面积为 ",self.area)
end

-- 创建对象
mysquare = Square:new(nil,10)
mysquare:printArea()

Rectangle = Shape:new()
-- 派生类方法 new
function Rectangle:new (o,length,breadth)
  o = o or Shape:new(o)
  setmetatable(o, self)
  self.__index = self
  self.area = length * breadth
  return o
end

-- 派生类方法 printArea
function Rectangle:printArea ()
  print("矩形面积为 ",self.area)
end

-- 创建对象
myrectangle = Rectangle:new(nil,10,20)
myrectangle:printArea()

Implementation of the above code, the output is:

面积为 	100
正方形面积为 	100
矩形面积为 	200

Function overrides

Lua, we can rewrite the base class function, define your own implementations in derived classes:

-- 派生类方法 printArea
function Square:printArea ()
  print("正方形面积 ",self.area)
end