Симуляция полиморфизма
Автор: Richard D. Clark
Code
'Simulated Polymorphism Using Method Pointers
'Richard D. Clark
'Requires the CVS version of FreeBasic
'**********************************************
#define isdog 1
#define iscat 2
Type animal
Public:
speak As Sub()
Declare Constructor (anid As Integer)
End Type
'Speak method for dog object
Sub Bark()
Print "Woof!"
End Sub
'Speak mehod for cat object
Sub Meow()
Print "Meow!"
End Sub
'Set the proper method pointer based on animal id
Constructor animal(anid As Integer)
If anid = isdog Then
This.speak = @Bark
ElseIf anid = iscat Then
This.speak = @Meow
End If
End Constructor
'Create a dog and cat object
Dim myDog As animal = isdog
Dim mycat As animal = iscat
'Have the animals speak
Print "My dog says ";
myDog.speak()
Print "My cat says ";
myCat.speak()
Sleep
End