class extended method sharing thing ;)

Monkey Forums/Monkey Beginners/class extended method sharing thing ;)

GC-Martijn(Posted 2015) [#1]
I want to use classes etc and now I'm wondering what is wisdom.

I have a 'base' class and a extended class.
The extended class will have the base class methods and some extra's methods.
But for example an extended class method will have the same functions in it like the base class but only with a little extra's.

The example....


Class Sprite
	Field img:Image
	Field Destination:Vector

	' just a basic clean method'
	Method MoveTo:Void(_mx:Float, _my:Float)
		Destination.Set(_mx,_my)	
	End
End

Class Player Extents Sprite

	' exactly the same as the Base class (Sprite) but with some extra things'
	Method MoveTo:Void(_mx:Float, _my:Float)
		' some extra player stuff '

		' and then I have to do the same as the the Base class (Sprite) MoveTo()'
		''
		Destination.Set(_mx,_my)	
	End
	Method Extra:Void()
	End
End


Local player:Player = New Player()
player.MoveTo(34.4,34.8)


This works, but I have to copy/past the base class function MoveTo and add that.
That is not the correct way ;)

Possible idea but a little 'ugly'

Class Sprite
	Field img:Image
	Field Destination:Vector

	' yust a basic clean method'
	Method MoveTo:Void(_mx:Float, _my:Float)
		Destination.Set(_mx,_my)	
	End
End

Class Player Extents Sprite

	' the same as the Base class (Sprite) but with some extra things'
	Method MoveTo2:Void(_mx:Float, _my:Float)
		' some extra player stuff '

		
		Self.MoveTo(_mx,_my) ' < acces the baseclass'
	End
	Method Extra:Void()
	End
End

Local player:Player = New Player()
player.MoveTo2(34.4,34.8)


Maybe there is a other way for doing this correct ?

Greetings,
GCMartijn


Danilo(Posted 2015) [#2]
See example for keyword Super: Super.MoveTo(_mx,_my)


GC-Martijn(Posted 2015) [#3]
Oke simple ;) din't see it when reading the class docs.