lua实现类和类的派生

MyClass = { m_N = 0 }
 
function MyClass:create()
    local o = {}
    setmetatable( o ,self)
    self.__index=self
 
    return o
end
 
function MyClass:SetN(n)
    self.m_N = n
end

SubClass = MyClass:create()
SubClass.m_B = 30

function SubClass:SetN(n)
    self.m_N = n*2
    self.m_B = n*2
end

 
local a = MyClass:create()
a:SetN(10)
print(a.m_N)

local b = SubClass:create()
b:SetN(10)
print(b.m_N)
print(b.m_B)

local c = SubClass:create()
c:SetN(40)
print(c.m_N)
print(c.m_B)

  

posted @ 2013-06-14 10:02  MrBlue  阅读(540)  评论(0编辑  收藏  举报