The Lua Minimalist Primer Guide (vii): Object-oriented Programming _lua

Source: Internet
Author: User

Class

In many object-oriented languages there are classes (class) concepts that object is an instance of a class. The concept of a class does not exist in Lua. Lua is a prototype-oriented language (http://en.wikipedia.org/wiki/Prototype-based_programming) like JavaScript, which uses an object to represent a "class". Other objects (instances of this class) use this object as a prototype. We have two table P and obj and set p to the prototype of obj (review: http://www.jb51.net/article/56690.htm):

Copy Code code as follows:

Setmetatable (obj, {__index = P})

Operations that do not exist in obj are found in P.

See a detailed example:

Copy Code code as follows:

Account = {
--The default value of balance
Balance = 0
}

function Account:new (o)
o = O or {}
--Set the prototype to account
Setmetatable (O, self)
Self.__index = Self
Return o
End

function Account:deposit (v)
Self.balance = self.balance + V
End

function Account:withdraw (v)
If v > self.balance Then
Print (' insufficient funds ')
Return
End
Self.balance = Self.balance-v
End

--Build Account object with initial balance of 100
Local A1 = Account:new{balance = 100}
A1:deposit (MB)--> balance = 200
A1:withdraw (MB)--> balance = 100

--Build the account object and use the default balance
Local a2 = Account:new ()
A2:deposit (MB)--> balance = 100

A colon can be used to add a hidden parameter self to a method when the method is defined by using a colon to pass the caller as an extra parameter to this method, for example:

Copy Code code as follows:

--The following two kinds of writing are equivalent
function Account:deposit (v)
function account.deposit (self, v)

--The following two kinds of writing are equivalent
A1:deposit (100)
A1.deposit (A1, 100)

Self is the caller of the method.

In the case of account, a small optimization is that we do not need to create an additional metatable and use account directly as a metatable.

Inherited

We use an example to explain how Lua implements inheritance. Suppose we need subclass Specialaccount inherited from Account,specialaccount can be overdrawn.

First, construct a subclass:

Copy Code code as follows:

Specialaccount = Account:new ()

Here Specialaccount has the new method of (inheriting) account. Then we can use Specialaccount to build the object:
Copy Code code as follows:

Local SA = Specialaccount:new{limit = 1000}
Sa:withdraw (100)

The object SA is constructed here through specialaccount:new, and the metatable of the SA is specialaccount. In Sa:withdraw, Lua finds no withdraw in the SA, finds it in Specialaccount, does not find withdraw in Specialaccount, and then in account find withdraw and call it. The withdraw in account is clearly not what we want, and we redefine it in Specialaccount:

Copy Code code as follows:

function Specialaccount:withdraw (v)
If V-self.balance >= self:getlimit () Then
Print (' insufficient funds ')
Return
End
Self.balance = Self.balance-v
End

function Specialaccount:getlimit ()
Return Self.limit or 0
End

We call Sa:withdraw again, and Lua finds withdraw in specialaccount first and calls it.

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.