Thread: ZO_Object
View Single Post
07/02/15, 02:08 PM   #6
XanDDemoX
AddOn Author - Click to view addons
Join Date: May 2015
Posts: 28
I use a slightly different function for class inheritance which requires a little less typing but it is essentially equivalent to ZO_Object

Code:
local function class(base)

	local c = {}
	
	if type(base) == "table" then 
		for k,v in pairs(base) do 
			c[k]=v
		end 
		c.base = base
	end 
	
	c.__index = c
	
	setmetatable(c,{ __call=function(self,...)

               -- create instance
		local obj = setmetatable({},c)
		if self.init then 
			self.init(obj,...)
		elseif base ~= nil and base.init ~= nil then 
			base.init(obj,...)
		end
		return obj
	end
	})
	return c
end
Usage

Code:
-- class
local Animal = class()

function Animal:init()

end


-- subclass
local Dog = class(Animal)

function Dog:init()
     self.base.init(self)
end


-- create new object instance
local dog1 = Dog()

Last edited by XanDDemoX : 07/02/15 at 02:17 PM.
  Reply With Quote