Currently I’m writing a game for Android using Corona SDK. And its main scripting language is LUA. My first impression of LUA
was that it is extremely easy to learn. However, being a lightweight scripting language, LUA doesnt support OOP in the direct way like Java/C++. Instead you have to go the round way using its metatable feature. Detail can be found via http://www.lua.org/pil/16.html.

So what if I want to implement my classes in separate files. It brought me surprise that there’s not much information about this on the Internet. So, feeling a bit frustrated, I decided to do it on my own.

First let’s start with the standard way. I.e. declare everything in your main.lua

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

    SpecialAccount = Account:new()
    s = SpecialAccount:new{limit=1000.00}
    function SpecialAccount:withdraw (v)
      if v - self.balance >= self:getLimit() then
        error"insufficient funds"
      end
      self.balance = self.balance - v
    end
    
    function SpecialAccount:getLimit ()
      return self.limit or 0
    end

    function s:getLimit ()
      return self.balance * 0.10
    end

Now let’s do the partitioning
Account class (account.lua)

module  (...,package.seeall)

function new (ret)
	local Account = {balance=0}

	function Account:new (ret) 
		ret = ret or {}
		setmetatable (ret,self)
		self.__index = self
		return ret
	end

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

	function Account:withdraw (amount)
		if (amount>self.balance) then
			print ("not sufficient money")
		else
			self.balance = self.balance - amount
		end
	end

	return Account:new (ret)
end

SpecialAccount (specialaccount.lua)

module (...,package.seeall)
Account = require ("account")

function new (arg)
	local SpecialAccount = Account.new (arg)
	
        -- method override
	function SpecialAccount:withdraw (amount)
		print ("asdf")
		if (amount>self.balance+self.getLimit()) then
			print ("not surficient money& limit")
		else
			self.balance = self.balance - amount
		end
	end

	function SpecialAccount:getLimit ()
		return self.limit
	end

	return SpecialAccount
end

To use these 2 classes, first we need to import them

SpecialAccount = require ("specialaccount")
Account = require ("account")

The rest is just to create their instances (notice the use of “.” not “:”) and calling corresponding methods

local s =  SpecialAccount.new ({balance=200,limit=500})
s:deposit (50)
s:withdraw (700)
print (s.balance)
local a = Account.new ({balance=100})
a:withdraw (101)
print (a.balance)
Advertisements