Difference between revisions of "Super"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "<c>super</c> is a keyword used to access base class methods from within a subclass method. Category:Language {{stub}}")
 
m (Fix yet another typo)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<c>super</c> is a [[keywords|keyword]] used to access base class methods from within a subclass method.
+
<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 class
 +
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.maxGrowth
 +
tree.grow
 +
wait .5
 +
end while
 +
</ms>
  
 
[[Category:Language]]
 
[[Category:Language]]
 +
[[Category:Keywords]]
  
 
{{stub}}
 
{{stub}}

Latest revision as of 22:11, 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 class
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.maxGrowth
	tree.grow
	wait .5
end while


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