Objected Oriented Lua (OO..Lala)

**under construction**

This is targeted towards someone new to Lua (like me) with more experience in a language like Java (like me). I didn't find a guide like this in my inter-web searching, so I hope it helps. I realize I'm using terms loosely and ignoring technicalities, but whatever dude.

Here is a basic example of how to OO..Lala (yes, I know, I'm amazingly creative):

This is all based on, and derived from this, but I ran mine.

main.lua

require 'account' 
require 'specialaccount' 

--instantiation example 

Bank = {} 

Bank.account1 = Account:new()
Bank.account2 = Account:new() 

Bank.account1:deposit(1000) 
Bank.account1:withdraw(500)
print(Bank.account1.balance)

Bank.account2:withdraw(1000) 
print(Bank.account2.balance)

--inheritance example 

Bank.account4 = SpecialAccount:new{limit=1000.00}
Bank.account4:withdraw(1000)
print(Bank.account4.balance)

The main file creates a table called Bank, instantiates a couple objects of type Account, plays with those objects before instantiating a SpecialAccount which inherits from Account.

account.lua

Account = {balance = 0} 
 
    function Account:new (_p) 
      _p = _p or {}
      setmetatable(_p, self)
      self.__index = self
      return _p
    end
 
    function Account:deposit (value)
      self.balance = self.balance + value
    end
    
    function Account:withdraw (value)
        if value > self.balance then
	 	print("insufficient funds")
        else       
	  	self.balance = self.balance - value
	end
    end

The Account is a basic example of a good way to make a Class. It's new function (which is like a constructor) is the hardest part to understand, but you could cut and paste it. Basically, an Account is a table with one value, balance, and 3 methods, new, deposit & withdraw.

specialaccount.lua

require 'account'

SpecialAccount = Account:new()

    function SpecialAccount:withdraw (value)
      if value - self.balance > self:getLimit() then
        print("insufficient funds")
      else 
      self.balance = self.balance - value
      end
    end
 
    function SpecialAccount:getLimit ()
      return self.limit or 0
    end

This shows how you can override inherited methods and setup new ones.

FYI, I'm writing programs with Corona to help my autistic son and other's like him. If you're interested in collaborating, hit me up.

Reserved

Also check out these videos on javascript ~ while not the same as lua, still very useful.

http://www.yuiblog.com/blog/2006/11/27/video-crockford-advjs/

-Angelo

Similar situation. Came over from VB 6 and medical apps. On my second corona app now, first was a simple free tip calculator (Split It!). This one is a physics puzzle.

My son is 20 and has aspergers syndrome (one end of the autistic spectrum)

There is a huge need to find ways to help these kids. Mine wants to write programs, but I need to make a path for him.

back at you...

A more thorough look at OOP with lua. http://lua-users.org/wiki/ObjectOrientedProgramming

I prefer using the class() implementation. It's cleaner than the __index, setmetatable() approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local obj = MyObject()
obj:init( 'justAFriendlyObject' )
print( obj.name, obj.inheritedProperty )
 
MyObject = class( MyBaseClass, function( self )
end)
 
MyBaseClass = class( function( self )
end)
 
function MyBaseClass:init( name )
        self.name = name
        self.inheritedProperty = 'inheritThis'
end

When deal with the Corona Display Object, it would be trouble because a new instance is not a Display Object that cannot be added into the DisplayGroup.

views:1598 update:2011/9/27 18:14:54
corona forums © 2003-2011