Difference between revisions of "Super"

From MiniScript Wiki
Jump to navigation Jump to search
(Added example code)
Line 1: Line 1:
 
<c>super</c> is a [[:Category:Keywords|keyword]] used to access base class methods from within a subclass method.  It is part of MiniScript's support for [[object-oriented programming]].
 
<c>super</c> is a [[:Category:Keywords|keyword]] used to access base class methods from within a subclass method.  It is part of MiniScript's support for [[object-oriented programming]].
 +
 +
 +
<b>Example code:</b>
 +
 +
<ms>
 +
//PLANT base clasee
 +
PLANT = new Sprite
 +
PLANT.image = Image.create(25, 25, color.lime)
 +
PLANT.maxGrowth = 20
 +
PLANT.scale = 0
 +
PLANT.grow = function
 +
self.scale  = self.scale + .1
 +
if self.scale > self.maxGrowth then
 +
self.scale = self.maxGrowth
 +
return
 +
end if
 +
end function
 +
 +
//tree subclass
 +
tree = new PLANT
 +
tree.x = 480
 +
tree.y = 320
 +
 +
tree.grow = function //trees function that would override grow within the PLANT base class
 +
super.grow //reaches out to the PLANT base class and calls it
 +
print "I grew!" //end of the function prints after super.grow is called.
 +
end function
 +
 +
//pushes tree to default sprite display
 +
display(4).sprites.push tree
 +
 +
//loop for tree.grow
 +
while not tree.scale
 +
tree.grow
 +
wait .5
 +
end while
 +
</ms>
  
 
[[Category:Language]]
 
[[Category:Language]]

Revision as of 22:04, 12 March 2022

super is a keyword used to access base class methods from within a subclass method. It is part of MiniScript's support for object-oriented programming.


Example code:

//PLANT base clasee
PLANT = new Sprite
PLANT.image = Image.create(25, 25, color.lime)
PLANT.maxGrowth = 20
PLANT.scale = 0
PLANT.grow = function
	self.scale  = self.scale + .1
	if self.scale > self.maxGrowth then
		self.scale = self.maxGrowth
		return
	end if
end function

//tree subclass
tree = new PLANT
tree.x = 480
tree.y = 320

tree.grow = function //trees function that would override grow within the PLANT base class
	super.grow //reaches out to the PLANT base class and calls it
	print "I grew!" //end of the function prints after super.grow is called.
end function

//pushes tree to default sprite display
display(4).sprites.push tree

//loop for tree.grow
while not tree.scale
	tree.grow
	wait .5
end while


This article is a stub. You can help the MiniScript Wiki by expanding it.