Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Bank class
- -- notice NO module(package.seecrap)
- local _M = {} -- class table, which is returned at the end
- -- properties
- _M.gold = 100 -- initial capital
- _M.silver = 50
- -- methods
- -- notice NO "local" and colons (:)
- -- those functions are methods of table _M and they use "self"
- function _M:withdraw(account, amount)
- self[account] = self[account] - amount
- return amount
- -- this is a faulty bank code, don't use it in real life
- end
- function _M:deposit(account, amount)
- self[account] = self[account] + amount
- end
- -- you can also use this as an exchange library
- -- notice NO "local" and a DOT (.) and no use of "self"
- function _M.gold2silver (amount)
- return amount * 2
- end
- -- when you require this thing you do bank = require("bank") and _M comes into "bank" variable
- return _M
- -- After you require you can use bank:deposite('silver', bank.gold2silver(bank.gold)) - which will convert all gold into silver inside the bank. Deposite is a method and gold2silver is a simple conversion function (supplimentary).
Add Comment
Please, Sign In to add comment