# Lua

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

function Account:deposit(v)    self.balance = self.balance + vend
– 用.来调用函数的话，需要手动给self传值。– 用冒号来调用函数的话，可以省略self。Account.deposit(Account, 200)Account:deposit(200)
````Account.withdraw(Account, 100)Account:withdraw(100)````

## Classes

if we have two objects A and B, all we have to do to make B a prototype for A is this:

``````setmetatable(A, {__index = B})
``````

After that, A looks up in B for any operation that it does not have.

Let us go back to our example of a bank account. To create other accounts with behavior similar to Account, we arrange for these new objects to inherit their operations from Account, using the __index metamethod.

``````local mt = {__index = Account}
function Account.new (o)
o = o or {} -- create table if user does not provide one
setmetatable(o, mt)
return o
end
``````

After this code, what happens when we create a new account and call a method on it, like this?

``````a = Account.new{balance = 0}
a:deposit(100.00)
``````

When we create the new account, a, it will have mt as its metatable. When we call a:deposit(100.00), we are actually calling a.deposit(a, 100.00); the colon is only syntactic sugar. However, Lua cannot find a “deposit” entry in the table a; hence, Lua looks into the __index entry of the metatable. The situation now is more or less like this:

``````getmetatable(a).__index.deposit(a, 100.00)
``````

The metatable of a is mt, and mt.__index is Account. Therefore, the previous expression evaluates to this one:

``````Account.deposit(a, 100.00)
``````

That is, Lua calls the original deposit function, but passing a as the self parameter. So, the new account a inherited the function deposit from Account. By the same mechanism, it inherits all fields from Account.

We can make two small improvements on this scheme. The first one is that we do not need to create a new table for the metatable role; instead, we can use the Account table itself for that purpose. The second one is that we can use the colon syntax for the new method, too. With these two changes, method new becomes like this:

``````function Account:new (o)
o = o or {}
self.__index = self
setmetatable(o, self)
return o
end
``````

Now, when we call Account:new(), the hidden parameter self gets Account as its value, we make Account.__index also equal to Account, and set Account as the metatable for the new object. It may seem that we do not gained much with the second change (the colon syntax); the advantage of using self will become apparent when we introduce class inheritance, in the next section.

## Inheritance 继承

``````Account = {balance = 0}

function Account:new(o)    o = o or {}    self.__index = self    setmetatable(o, self)    return oend
function Account:deposit(v)    self.balance = self.balance + vend
function Account:withdraw(v)    if v > self.balance then error&#34;insufficient funds&#34; end    self.balance = self.balance - vend
SpecialAccount = Account:new()s = SpecialAccount:new{limit = 1000.00}
function SpecialAccount:withdraw(v)    if v - self.balance >= self.getLimit() then        error&#34;insufficient funds&#34;    end    self.balance = self.balance - vend
````function SpecialAccount:getLimit()    return self.limit or 0end```` 