This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Neander_King's profile picture

Class Inheritance

Started by Neander_King, 20 April 2015 - 07:42 PM
Neander_King #1
Posted 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:

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".
Edited on 20 April 2015 - 07:43 PM
flaghacker #2
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.
Edited on 20 April 2015 - 08:12 PM
Creator #3
Posted 20 April 2015 - 10:32 PM
Take a look at this to see how oop works. From there you may see the mechanics
Bomb Bloke #4
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?
Creator #5
Posted 21 April 2015 - 06:40 AM
Here is some oop:


--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)
Lupus590 #6
Posted 21 April 2015 - 07:53 AM
Ahhh my eyes!!!!

:


--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
that's better
Edited on 21 April 2015 - 05:53 AM
Creator #7
Posted 21 April 2015 - 12:55 PM
Ahhh my eyes!!!!

:


--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
that's better

I kind of assume people know what they are doing and are not gonna make textPos greater than lenght/width
Kouksi44 #8
Posted 21 April 2015 - 05:02 PM
There´s a pretty neat tutorial about inheritance here : http://lua-users.org/wiki/InheritanceTutorial