Rcjp's Weblog

June 6, 2006

LUA and OOP

Filed under: misc — rcjp @ 3:52 pm

Its interesting how LUA can simulate oop by just using tables. (the following is from the manual)…

    --
    -- Define an Account object
    --
    Account = {}

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

    --
    -- An Account instance
    --

    a = {balance=50.0, withdraw = Account.withdraw}

    a:withdraw(100.00)
    print("balance = ", a.balance)


    --
    -- do static methods, variables with metatables
    -- 

    Account = {balance = 0}   -- default value for balance

    function Account:new (o)
       o = o or {}   -- create object if user does not provide one
       setmetatable(o, self)
       self.__index = self
       return o
    end

    function Account:deposit (v)
       self.balance = self.balance + v
    end

    a = Account:new{balance = 5}  -- sets a = table with balance=5 and metatable with
                                  -- balance of 0, so this overrides the default
    a:deposit(100.00)
    Account.deposit(a, 100.00)  -- same thing as previous line
    print("a balance = ", a.balance)

    b = Account:new()  -- balance not set so will pick up 0 from above
    print("b balance = ", b.balance)

    --
    -- Inheritance
    --
    SpecialAccount = Account:new()
    s = SpecialAccount:new{limit=1000.00} -- the self parameter will refer to SpecialAccount
                                          -- so the metatable of s will be SpecialAccount

    -- redefine superclass methods

    function SpecialAccount:withdraw (v)
       if v - self.balance >= self:getLimit() then
          print "insufficient funds"
       else
          self.balance = self.balance - v
       end
    end

    function SpecialAccount:getLimit ()
       return self.limit or 0
    end

    -- so with a special account you can go overdrawn
    s:deposit(100.00)
    s:withdraw(200.00)
    print("s balance = ", s.balance)

    -- If only a single object needs a specific behavior
    -- with Lua you do not need to create a new class to specify a new behavior
    -- just implement the necessary function in the _instance_ itself...

    James = SpecialAccount:new{}
    James:deposit(100.0)

    -- James' limit is always 10% of his previous balance
    function James:getLimit ()
       return self.balance * 0.10
    end

    James:withdraw(109.00)
    print("Jame balance = ", James.balance)
    James:withdraw(10.00)
    print("Jame balance = ", James.balance)

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: