Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Module_classExtension = {}
- function Module_classExtension.addParentClass(Class_targetClass,Class_newClass)
- table.insert(getmetatable(Class_targetClass).__index._proxy,Class_newClass)
- --[[
- insert Class_newClass into the table with _proxy in it. _proxy contains the parent classes
- ]]
- end
- function Module_classExtension.removeParentClass(Class_targetClass,Class_classToRemove)
- local Tab_proxyTable = getmetatable(Class_targetClass).__index._proxy
- for Int_giPosition,Class_parentClass in pairs(Tab_proxyTable) do
- if Class_parentClass == Class_classToRemove then
- table.remove(Tab_proxyTable,Int_giPosition)
- end
- end
- --[[
- get the _proxy table
- iterate through the proxy table, and
- if the class in the proxy table is the class we need to remove then
- remove it
- ]]
- end
- function Module_classExtension.findMethodFromParentClasses(Tab_proxyTable,Func_method)
- for Int_giIndex,Class_gvClass in pairs(Tab_proxyTable._proxy) do
- if Class_gvClass[Func_method] then
- return Class_gvClass[Func_method]
- end
- end
- --[[
- iterate through _proxy in Tab_proxyTable,
- if one of the classes has the method then
- return the method
- ]]
- end
- function Module_classExtension.getParentClasses(Class_targetClass)
- return getmetatable(Class_targetClass).__index._proxy
- --[[
- return the the classes in _proxy
- ]]
- end
- function Module_classExtension.createMultipleInheritance(Class_baseClass)
- setmetatable(Class_baseClass,{__index = setmetatable({_proxy = {}},{__index = Module_classExtension.findMethodFromParentClasses})})
- --[[
- this allows multiple inheritance
- the structure is:
- Class ==_index==> {_proxy = {parent1,parent2,...}} ==_index==> function findMethodFromParentClasses
- ]]
- end
- --================================================================================================================
- --================================================================================================================
- local Class_drawing = {}
- function Class_drawing:newShape(String_name)
- return {["String_shape"] = String_name}
- end
- function Class_drawing:drawShape()
- print(self["String_shape"])
- end
- --================================================================================================================
- --================================================================================================================
- local Class_quadrilateral = {}
- Module_classExtension.createMultipleInheritance(Class_quadrilateral)
- Module_classExtension.addParentClass(Class_quadrilateral,Class_drawing)
- function Class_quadrilateral:someMethod()
- print("HI")
- end
- --================================================================================================================
- --================================================================================================================
- local Class_square = {}
- Module_classExtension.createMultipleInheritance(Class_square)
- Module_classExtension.addParentClass(Class_square,Class_quadrilateral)
- function Class_square:newSquare()
- return setmetatable(self:newShape("Square"),{__index = self})
- end
- --================================================================================================================
- --================================================================================================================
- local Square_newSquare = Class_square:newSquare()
- Square_newSquare:drawShape()
- Square_newSquare:someMethod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement