Extended types and calling Methods/Fields
Monkey Forums/Monkey Programming/Extended types and calling Methods/Fields
| ||
I would like to call a method in my Base class that also grabs values from the associated extended Class, when I do it says it cannot find the extended fields. For example I want to Print the value of a (baseclass) and b (extendedclass) from a method in the BaseClass but I get then error "Identifier "b" not found". Seems a base Class Method cannot reference what is in an extended class, is that by design? Class BaseClass Field a = 1 Method Output Print a Print b End Method End Class Class ExtendedClass extends BaseClass Field b = 2 End Class |
| ||
Yes, it is by design. The base class cannot see the 'b' field because it is not defined in the base class. You can put the output method in your extended class, as that class can see both fields. |
| ||
Thanks wiebo Such a shame I kinda relied on calling the methods from my Base class, oh well I'll rewrite it with this in mind. |
| ||
Unless BaseClass actually contains a b field, or extends a class that contains a b field, how would it know whether it exists or not? Can you not do.. ? Class BaseClass Field a = 1 Field b = 1 Method Output Print a Print b End Method End Class Class ExtendedClass extends BaseClass Field b = 2 End Class |
| ||
Then: why extend? =] |
| ||
I think you are not getting the extend part ;) Say you have a class "car" then a class "super car", super car would extend car and it would gain all the elements of car. Class Car Field frontLeftWheel Field frontRightWheel Field backLeftWheel Field backRightWheel End Class SuperCar Extends Car Field booster Field wings End So a car has four wheels, and the super car has four wheels and wings and a booster... car does not ;) It sounds like you need to use interfaces... |
| ||
Then: why extend? =] I use this kind of thing to have a default action / value for a particle system that's easily changed from within the extended class. It's probably not entirely necessary, but it's manageable. |
| ||
you know you can do this. Right?:Class BaseClass Field a = 1 Field b = 1 Method Output() Abstract End Class Class ExtendedClass Extends BaseClass Field b = 2 Method Output Print a Print b End Method End Class Function Main() Local myClass:BaseClass = extendedClass myClass.Output() End note that myClass is a BaseClass . |