Casting objects - Polymorphism

BlitzMax Forums/BlitzMax Beginners Area/Casting objects - Polymorphism

Using those Methods (usually called Setter and Getter) are used in many OO-Languages.

It gives you the opporunity to Check any of the values you want to set.
It gives you the opporunity to return values independent of your base implementation of the class.

One of the Basic priniples in OO ist to keep the inner values a secret, and let in only change by the methods you want it to.

unfortunately this is not really implemented in bmax, c# and java use modiefiers so you can simply change the permission of every single class member

SuperStrict

Type TAlien
	Field health:Int = 1

	Method PrintHealth() Abstract

	Method GetHealth:Int() Abstract
	
	Method SetHealth(health:Int) Abstract
		

EndType	

Type TAlienSmall Extends TALien
  	Field health:Int = 2

  	Method PrintHealth()
		Print "Method PH in TAlienSmall"
	End Method

	Method GetHealth:Int()
		Return health
	EndMethod
	
	Method SetHealth(health:Int)
		Self.health = health
	EndMethod

End Type

Type TAlienBig Extends TAlien
	Field health:Int = 3
	
	Method PrintHealth()
    		Print "Method PH in TAlienBig"
    	End Method
	
	Method GetHealth:Int()
		Return health
	EndMethod
	
	Method SetHealth(health:Int)
		Self.health = health
	EndMethod

End Type

' Main
Local alienlist:TList = New TList
Local as:Talien = New TAlienSmall
Local ab:TAlien = New TAlienBig

alienlist.AddLast(as)
alienlist.AddLast(ab)

For Local alien:TAlien = EachIn alienlist
	alien.PrintHealth
	Print alien.GetHealth()
Next