Extended types and calling Methods/Fields

Monkey Forums/Monkey Programming/Extended types and calling Methods/Fields

Supertino(Posted 2011) [#1]
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



wiebow(Posted 2011) [#2]
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.


Supertino(Posted 2011) [#3]
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.


Raz(Posted 2011) [#4]
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



wiebow(Posted 2011) [#5]
Then: why extend? =]


therevills(Posted 2011) [#6]
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...


Raz(Posted 2011) [#7]
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.


Jesse(Posted 2011) [#8]
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


.