Polymorphism in monkey?
Monkey Forums/Monkey Programming/Polymorphism in monkey?
| ||
How does polymorphism work in monkey? Is it supported? For instance for organising different types of objects. Maybe it's just too late as well. |
| ||
The Monkey docs talks about generics a bit, so it is supported to some degree. |
| ||
There's classes with superclasses, and casting between them... But there's no interfaces or multiple inheritence - although "interface" is a reserved keyword apparently. |
| ||
Still tired... Do you have any example with the usual apple and banana, both declared as fruits? So far all worked out fine with building classes out of classes, extending them and using abstract methods (although reserved you must not use it) but this way you can't access variables from the apples and bananas which aren't defined in the fruits class anymore. |
| ||
The child classes does not inherites the master methods? Class Sprite Method New(img:Image,x#, y#,frame:Int=0) Self.image = img Self.x = x Self.y = y Self.w = img.Width() Self.h = img.Height() Self.w2 = Self.w/2 Self.h2 = Self.h/2 Self.currentframe=frame End EndIf Class Amebo extends Sprite Endif Local a:Amebo=New Amebo(image_amebo,100,100,0) reaises me a "Unable to find overload for new(expression,100,100,0) I have not changed the new method in the Amebo Class, Can't I use the new method from the Sprite class? |
| ||
Wagenheimer, You will need to call the base class from the New constructor like this: Class Amebo extends Sprite Method New() Super.New(image_amebo,100,100,0) End Method Endif |
| ||
Thanks dopeyrulz! Works very well! =) |
| ||
Oh, this used to happen automatically in blitmax, afaik. |
| ||
It happened automatically when New didn't accept any parameters, and that's still the case in Monkey - if you define a New() method in the parent class with no parameters, that automatically gets called. |