--[[
lua是没有类的,但是可以用元表模仿类和类的继承
--]]
--@file Account.lua
--@desc 类Account
local Account = {
balance = 0
}
function Account:new(o)
o = o or {}
--lua使用元表实现类,继承
setmetatable(o, self)
self.__index = self
return o
end
--等价于function Account.deposit(self, v)
function Account:deposit( v )
print("user deposit", v)
self.balance = self.balance + v
end
function Account:withdraw( v )
print("user withdrwa", v)
if v > self.balance then
error "insufficient funds"
end
self.balance = self.balance - v
end
function Account:getBalance( )
print("user have balance", self.balance)
return self.balance
end
return Account
-----------------------------------------------
--@file SpecialAccount
--@desc 类SpcialAccount,继承类Account
local Account = require("Account")
local SpecialAccount = {
limit = 1000
}
function SpecialAccount:new( o )
o = o or {}
setmetatable(o, self)
self.__index = self
Account:new(self)
return o
end
function SpecialAccount:deposit( v )
local curr = self.balance + v
if curr > self:getLimit() then
error "outnumber limit"
end
self.balance = curr
end
function SpecialAccount:getLimit( )
print("user limit is", self.limit)
return self.limit or 0
end
return SpecialAccount
-----------------------------------------
--@file User.lua
--@desc 类User
local User = {
name = ""
}
function User:new( o )
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function User:setName( name )
print("user change name")
self.name = name
end
function User:getName( name )
print("user name is:", self.name)
return self.name
end
return User
--------------------------------------------
--@file CreateClass.lua
--@desc 提供createClass方法,该方法可实现多重继承
package.path = 'F:/workspace/Lua/?.lua;' --读取其他路径目录下的lua文件
local dump = require("dump")
local function search( key, list )
for i = 1, table.getn(list) do
if type(list[i]) == "table" then
local value = list[i][key]
if value then
return value
end
end
end
end
--可变参数“...”和“arg”
local function createClass( ... )
local c = {}
setmetatable(c, {__index = function ( t, k )
return search(k, arg)
end})
c.__index = c
function c:new( o )
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
return c
end
return createClass
-----------------------------------------
--@file AccountUser.lua
--@desc 类AccountUser,继承类Account和类User
local Account = require("Account")
local User = require("User")
local class = require("CreateClass")
local AccountUser = class(Account, User)
function AccountUser:getName( )
print("AccountUser getName:", self.name)
return self.name
end
return AccountUser
---------------------------------------------------
--------------- test ---------------------------------------
local Account = require("Account")
local SpecialAccount = require("SpecialAccount")
local AccountUser = require("AccountUser")
function main( )
----------------- test class -----------------------------
local classTest = Account:new()
classTest:deposit(5)
classTest:withdraw(4)
classTest:getBalance()
--------------- test inherit ----------------------------------
-- SpecialAccount inherit Account
local inheritTest = SpecialAccount:new({limit = 10})
inheritTest:getLimit()
inheritTest:getBalance()
inheritTest:deposit(5)
inheritTest:getBalance()
inheritTest:deposit(10)
inheritTest:getBalance()
--------------- test multiple inheritance --------------------
-- AccountUser inherit Account and User
local mulInheritTest = AccountUser:new()
mulInheritTest:setName("huang")
mulInheritTest:getName()
mulInheritTest:deposit(50)
end
main()