On the object-oriented characteristics of Lua _lua

Source: Internet
Author: User
Tags access properties extend inheritance lua

Object-oriented features

    • Class: A class is an extensible template used to create objects that provide the initial values (member variables) of the State and the implementation of the behavior.
    • Object: It is an instance of a class and has memory allocated to itself.
    • Inheritance: It is a concept that is inherited by the functions of variables and classes by other classes.
    • Encapsulation: It is a method of combining data and functions in a class. Data can be accessed outside the class with the help of a function. It is also called data abstraction.

The LUA OOP

Implement the first class of object-oriented and table and LUA functions in Lua. An object is formed by inserting functions and related data into a table. Inheritance can be implemented with the help of Metatables, providing a lookup mechanism that does not exist in a function (method) and in a Parent object field.

In the LUA table there is such a state and identity object, which is independent of the value of the attribute. Two objects (tables) that have the same value but are in different objects, while an object can have a different value, but it is always the same object. Just like there is a life cycle in the object table, created or created independently.
A real-world example

The object-oriented concept is broad, but it is important to understand and derive the greatest benefit.

Let's consider a simple mathematical example. We often encounter situations in which we work in different shapes like circles, rectangles and squares.

Shapes can have a common property area. Therefore, we can extend the other shapes from the underlying object shape with the common attribute area. Each shape can have its own properties and functions similar to the rectangle can have attributes of length, width, area as its properties, PrintArea neutralization CalculateArea as its function.
to create a simple class

A simple class implements the rectangular three property area, the length and width shown below. It also has a printarea function to print the calculated area.

Copy Code code as follows:
--Meta class
Rectangle = {area = 0, length = 0, breadth = 0}

--Derived class method 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

--Derived class method PrintArea
function Rectangle:printarea ()
Print ("The area of Rectangle is", Self.area)
End

Creating objects

Creating an object is the process of allocating storage to an instance of a class. Each object has its own storage and shared common class data.

Copy Code code as follows:
R = rectangle:new (nil,10,20)

Access Properties

Use points in a class. operator, as shown in the following figure, to access the property

Copy Code code as follows:
Print (R.length)

Accessing member functions

Using the colon operator, you can access an object member function as shown in the following illustration.

Copy Code code as follows:
R:printarea ()

The memory is assigned and the initial value is set. The initialization process can be compared to other object-oriented language constructs. It's just a function set value, as shown in the previous illustration.
Complete Example

Let's take a look at a complete example of using object-oriented Lua.

Copy Code code as follows:
--Meta class
Shape = {area = 0}

--Base class method 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

--Base class method PrintArea
function Shape:printarea ()
Print ("The" is ", Self.area)
End

--Creating an object
Myshape = Shape:new (nil,10)

Myshape:printarea ()

When you run the above program, you will get the following output.

Copy Code code as follows:
The area is 100

The inheritance of Lua

Inheritance is the basic object of expanding the shape simply, with rectangular, square, etc. processing. It is commonly used for the basic nature and functionality of sharing and scaling in the real world.

Let's look at a simple class extension. There is a class, as shown in the following illustration.

Copy Code code as follows:
--Meta class
Shape = {area = 0}
--Base class method 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
--Base class method PrintArea
function Shape:printarea ()
Print ("The" is ", Self.area)
End

We can extend the shape to a square class as shown below.

Copy Code code as follows:
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

Overloading the underlying function

We can overload the base class function by using a function in the base class, rather than deriving the class itself, as shown in the following figure

Copy Code code as follows:
--Derived class method PrintArea
function Square:printarea ()
Print ("The area of Square is", Self.area)
End

Inherit the complete example

The simple class implementation we can extend in Lua, as shown in the figure above, metatables another new method. All the member variables and the functions of the base class are kept in the derived class.

Copy Code code as follows:
--Meta class
Shape = {area = 0}
--Base class method 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
--Base class method PrintArea
function Shape:printarea ()
Print ("The" is ", Self.area)
End

--Creating an object
Myshape = Shape:new (nil,10)
Myshape:printarea ()

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

--Derived class method PrintArea
function Square:printarea ()
Print ("The area of Square is", Self.area)
End

--Creating an object
MySQUARE = Square:new (nil,10)
Mysquare:printarea ()

Rectangle = Shape:new ()
--Derived class method 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

--Derived class method PrintArea
function Rectangle:printarea ()
Print ("The area of Rectangle is", Self.area)
End

--Creating an object
MyRectangle = Rectangle:new (nil,10,20)
Myrectangle:printarea ()

When we run the above program, we get the output below.

Copy Code code as follows:
The area is 100
The area of Square is 100
The area of Rectangle is 200

In the example above, we created two derived classes rectangle and square from the base class square. Therefore, derived classes are able to change the functionality of the base class here. In this implementation example, the derived class replaces the function PrintArea.

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.