当前位置 : 主页 > 网络编程 > lua >

lua流水账4:面向对象

来源:互联网 收集:自由互联 发布时间:2021-06-23
1.类的示例: Account = { balance = 0 ; --self相当于 this withdraw = function (self,v) print ( "this is withdraw" ) ; self.balance = self.balance - v; end}--使用:可以隐藏self function Account :deposit (v) print ( "this is dep

1.类的示例:

Account = {
    balance = 0;
    --self相当于this
    withdraw = function(self,v) print("this is withdraw");
        self.balance = self.balance - v;
    end
}
--使用:可以隐藏self
function Account:deposit(v) print("this is deposit");
    self.balance = self.balance + v;
end

Account.deposit(Account,200.00);
Account.withdraw(Account,100.00);
Account:deposit(200.00);
输出是:this is deposit
this is withdraw
this is deposit
Account = {
    balance = 0;
    list = {};
    ownList = {};
}

function Account:new(o) o = o or {};
    setmetatable(o,self);
    self.__index = self;
    return o;
end
function Account:deposit(v) print("this is deposit");
    self.balance = self.balance + v;
end

function Account:addList(key,value) self.list = self.list or {};
    self.list[key] = value;
end
function Account:addOwnList(key,value) self.ownList = self.ownList or {};
    self.ownList[key] = value;
end

a = Account:new{balance = 0,ownList = {}}
a:deposit(100.00)
print(a.balance);
print(Account.balance);
a:addList("key","value");
print(a.list["key"]);
print(Account.list["key"]);
a:addOwnList("key","value");
print(a.ownList["key"]);
print(Account.ownList["key"]);
输出是:this is deposit
100.0
0
value
value
value
nil

2.类继承

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

function Account:deposit(v)
    print("this is Account:deposit");
    self.balance = self.balance + v;
end

function Account:withdraw(v)
    print("this is Account:withdraw")
    if v > self.balance then error "insufficient funds" end
    self.balance = self.balance - v;
end

SpecialAccount = Account:new();

function SpecialAccount:withdraw(v)
    print("this is SpecialAccount:withdraw");
    if v - self.balance > self:getLimit() then
        error "insufficient funds";
    end
    self.balance = self.balance - v;
end
function SpecialAccount:getLimit()
    print("this is SpecialAccount:getLimit");
    return self.limit or 0;
end

s = SpecialAccount:new{limit = 1000.00}
function s:getLimit()
    print("this is s:getLimit");
    return self.balance * 0.10;
end
s:deposit(100.00);
s:withdraw(10);
输出是:this is Account:deposit
this is SpecialAccount:withdraw
this is s:getLimit

3.多重继承

local function search(k,plist)
    for i = 1,table.getn(plist) do
        local v = plist[i][k];
        if v then return v end;
    end
end

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,c);
        return o;
    end
    return c;
end

Named = {};
function Named:getname()
    return self.name;
end

function Named:setname()
    self.name = n;
end

Account = {balance = 0};

NamedAccount = createClass(Account,Named);
account = NamedAccount:new{name = "Paul"};
print(account:getname());

4.类的私有性

function newAccount(initialBalance)
    local self = {
        balance = initialBalance,
        LIM = 10000.00,
        };
    local withdraw = function(v)
        self.balance = self.balance - v;
    end
    local deposit = function(v)
        self.balance = self.balance + v;
    end
    local extra = function()
        if self.balance > self.LIM then
            return self.balance * 0.10;
        else
            return 0;
        end
    end
    local getBalance = function() return self.balance + extra() end;
    return {
        withdraw = withdraw,
        deposit = deposit;
        getBalance = getBalance
    }
end

acc1 = newAccount(100.00);
acc1.withdraw(40.00);
print(acc1.getBalance());

5.Single-Method即对象只有一个单一的方法,这种情况下,我们不需要创建一个接口表,取而代之的是,我们将这个单一的方法作为对象返回。

function newObject(value)
    return function(action,v)
        if action == "get" then return value;
        elseif action == "set" then value = v;
        else error("invalid action");
        end
    end
end

d = newObject(0);
print(d("get"));
d("set",10);
print(d("get"));
上一篇:Lua面向对象 --- 多继承
下一篇:lua学习
网友评论