I have been reading up on the may ways to do OOP with Lua and Corona and some seem very complicated and some to miss the whole point entirely. The tutorial here: http://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/whilst good appears limited when discussing Polymorphism and Inheritance. I have cobbled together a very basic test and I was wondering whether people might be able to critique it and point out flaws or improvements?
main.lua
--[[This is to demonstrate creating abstract Classes with the ability to instantiate multiple independent instances. Person is the Super Class and Child is the subclass]]-- local Person = require( "Person" ) -- include the Person class local Child = require( "Child" ) -- include the Person class local myPersonOne = Person:new() -- create an instance of the Person Class local myPersonTwo = Person:new() -- create an instance of the Person Class local myChildOne = Child:new() -- create an instance of the Child Class local myChildTwo = Child:new() -- create an instance of the Child Class -- BEFORE NAMES SET print( "myPersonOne:" ) print( myPersonOne ) print( "myPersonOne.name:" ) print( myPersonOne:getFirstName() ) print( "-----------------------" ) print( "myPersonTwo:" ) print( myPersonTwo ) print( "myPersonTwo.name:" ) print( myPersonTwo:getFirstName() ) print( "-----------------------" ) print( "myChildOne:" ) print( myChildOne ) print( "myChildOne.name:" ) print( myChildOne:getFirstName() ) print( "-----------------------" ) print( "myChildTwo:" ) print( myChildTwo ) print( "myChildTwo.name:" ) print( myChildTwo:getFirstName() ) print( "-----------------------" ) -- SET THE NAMES myPersonOne:setFirstName( "Oliver" ) myPersonTwo:setFirstName( "Dave" ) myChildOne:setFirstName( "Steve" ) myChildTwo:setFirstName( "Mike" ) -- AFTER NAMES SET print( "myPersonOne:" ) print( myPersonOne ) print( "myPersonOne.name:" ) print( myPersonOne:getFirstName() ) print( "-----------------------" ) print( "myPersonTwo:" ) print( myPersonTwo ) print( "myPersonTwo.name:" ) print( myPersonTwo:getFirstName() ) print( "-----------------------" ) print( "myChildOne:" ) print( myChildOne ) print( "myChildOne.name:" ) print( myChildOne:getFirstName() ) print( "-----------------------" ) print( "myChildTwo:" ) print( myChildTwo ) print( "myChildTwo.name:" ) print( myChildTwo:getFirstName() )
Person.lua
local Person = { firstName = nil } ------------------------------------------------- -- CONSTRUCTOR ------------------------------------------------- function Person:new ( \_object ) object = \_object or {} -- create object if user does not provide one setmetatable( object, self ) self.\_\_index = self return object end ------------------------------------------------- -- PRIVATE FUNCTIONS ------------------------------------------------- ------------------------------------------------- -- PUBLIC FUNCTIONS ------------------------------------------------- function Person:setFirstName ( \_firstName ) self.firstName = \_firstName end function Person:getFirstName () return self.firstName end return Person
Child.lua
-- initially make this a new instance of Person local Child = require( "Person" ):new(); ------------------------------------------------- -- CONSTRUCTOR ------------------------------------------------- function Child:new ( \_object ) object = \_object or {} -- create object if user does not provide one setmetatable(object , self ) self.\_\_index = self return object end ------------------------------------------------- -- PRIVATE FUNCTIONS ------------------------------------------------- ------------------------------------------------- -- PUBLIC FUNCTIONS ------------------------------------------------- function Child:setFavouriteToy ( \_favouriteToy ) self.favouriteToy = \_favouriteToy end function Child:getFavouriteToy () return self.favouriteToy end function Child:setFirstName ( \_firstName ) -- this method overwrites Person.setFirstName() self.firstName = \_firstName .. " OVERWRITTEN!" end return Child
Any feedback would be really appreciated. Next I want to look into extending DisplayObjects based on this model and custom events.
Thanks in advance.