function newClass(index,...)
local new_class = {}
setmetatable(new_class,{__index = index})
local optionalArgs = {...}
if optionalArgs[1] ~= nil then
setmetatable(new_class,optionalArgs[1])
end
return new_class
end
--TESTINGCODE
exampleSuper = newClass({name="Super",id=1,getName = function() print("Super") end,})
exampleSuper.getName()
exampleSub = newClass({name="Sub",})
print(exampleSub.id)
However, when I run this, exampleSub.id shows up blank. I want it to inherit any fields from it's parent class, so that I could call exampleSub.getName() and expect the output of "Super".
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Class Inheritance
Started by Neander_King, 20 April 2015 - 07:42 PMPosted 20 April 2015 - 09:42 PM
I'm trying to design an api where the user may create their own classes much more quickly than if they had to do it manually. However, I don't know how to achieve inheritance in Lua. Here's what I tried:
Edited on 20 April 2015 - 07:43 PM
Posted 20 April 2015 - 10:11 PM
The lua reference manual has an entire (very interesting) chapter on the subject of object-oriented programming:
http://www.lua.org/pil/16.html
It covers a lot of aspects of oop, such as private variables and inheritance.
http://www.lua.org/pil/16.html
It covers a lot of aspects of oop, such as private variables and inheritance.
Edited on 20 April 2015 - 08:12 PM
Posted 20 April 2015 - 10:32 PM
Take a look at this to see how oop works. From there you may see the mechanics
Posted 20 April 2015 - 11:47 PM
exampleSub = newClass({name="Sub",}) -- Your "parent class" here only has a "name" field,
print(exampleSub.id) -- ... so this is going to be nil, isn't it?
Posted 21 April 2015 - 06:40 AM
Here is some oop:
With the new method I can initialize a new instance of button by doing this:
Since mybutton gets the button methods as a metatable, I can do this:
--Class Button--
--Button initialization function
Button.new = function(input)
local self = {}
setmetatable(self,{__index = Button})
self.name = input.name
self.label = input.label
self.xPos = input.xPos
self.yPos = input.yPos
self.fgColor = input.fgColor
self.bgColor = input.bgColor
self.xLength = input.xLength
self.yLength = input.yLength
self.returnValue = input.returnValue
self.xTextPos = input.xTextPos
self.yTextPos = input.yTextPos
self.onRightClick = input.onRightClick or nil
self.onLeftClick = input.onLeftClick or nil
return self
end
--Draw function
Button.draw = function(self,addX,addY)
addX = addX or 0
addY = addY or 0
local finalX = self.xPos + addX
local finalY = self.yPos + addY
self.finalX = finalX
self.finalY = finalY
local newText
if #self.label > self.xLength then
newText = string.sub(self.label,1,self.xLength)
else
newText = self.label
end
paintutils.drawFilledBox(finalX,finalY,finalX+self.xLength-1,finalY+self.yLength-1,self.bgColor)
term.setTextColor(self.fgColor)
term.setCursorPos(finalX+self.xTextPos-1,finalY+self.yTextPos-1)
term.write(newText)
end
--Return function--
Button.returnData = function(self)
local toReturn = {}
for i,v in pairs(self) do
toReturn[i] = v
end
return toReturn
end
With the new method I can initialize a new instance of button by doing this:
mybutton = Button.new(argumentsYay)
Since mybutton gets the button methods as a metatable, I can do this:
anotherButton = myButton.new(wowArgs)
Posted 21 April 2015 - 07:53 AM
Ahhh my eyes!!!!
that's better:--Class Button-- --Button initialization function Button.new = function(input) local self = {} setmetatable(self,{__index = Button}) self.name = input.name self.label = input.label self.xPos = input.xPos self.yPos = input.yPos self.fgColor = input.fgColor self.bgColor = input.bgColor self.xLength = input.xLength self.yLength = input.yLength self.returnValue = input.returnValue self.xTextPos = input.xTextPos self.yTextPos = input.yTextPos self.onRightClick = input.onRightClick or nil self.onLeftClick = input.onLeftClick or nil return self end --Draw function Button.draw = function(self,addX,addY) addX = addX or 0 addY = addY or 0 local finalX = self.xPos + addX local finalY = self.yPos + addY self.finalX = finalX self.finalY = finalY local newText if #self.label > self.xLength then newText = string.sub(self.label,1,self.xLength) else newText = self.label end paintutils.drawFilledBox(finalX,finalY,finalX+self.xLength-1,finalY+self.yLength-1,self.bgColor) term.setTextColor(self.fgColor) term.setCursorPos(finalX+self.xTextPos-1,finalY+self.yTextPos-1) term.write(newText) end --Return function-- Button.returnData = function(self) local toReturn = {} for i,v in pairs(self) do toReturn[i] = v end return toReturn end
Edited on 21 April 2015 - 05:53 AM
Posted 21 April 2015 - 12:55 PM
Ahhh my eyes!!!!that's better:--Class Button-- --Button initialization function Button.new = function(input) local self = {} setmetatable(self,{__index = Button}) self.name = input.name self.label = input.label self.xPos = input.xPos self.yPos = input.yPos self.fgColor = input.fgColor self.bgColor = input.bgColor self.xLength = input.xLength self.yLength = input.yLength self.returnValue = input.returnValue self.xTextPos = input.xTextPos self.yTextPos = input.yTextPos self.onRightClick = input.onRightClick or nil self.onLeftClick = input.onLeftClick or nil return self end --Draw function Button.draw = function(self,addX,addY) addX = addX or 0 addY = addY or 0 local finalX = self.xPos + addX local finalY = self.yPos + addY self.finalX = finalX self.finalY = finalY local newText if #self.label > self.xLength then newText = string.sub(self.label,1,self.xLength) else newText = self.label end paintutils.drawFilledBox(finalX,finalY,finalX+self.xLength-1,finalY+self.yLength-1,self.bgColor) term.setTextColor(self.fgColor) term.setCursorPos(finalX+self.xTextPos-1,finalY+self.yTextPos-1) term.write(newText) end --Return function-- Button.returnData = function(self) local toReturn = {} for i,v in pairs(self) do toReturn[i] = v end return toReturn end
I kind of assume people know what they are doing and are not gonna make textPos greater than lenght/width
Posted 21 April 2015 - 05:02 PM
There´s a pretty neat tutorial about inheritance here : http://lua-users.org/wiki/InheritanceTutorial