KCC , Voici un autre exemple un peu plus "parlant"
Code : Tout sélectionner
Interface Vector2f
Add(Other.Vector2f)
GetX.f()
GetY.f()
DotProduct.f(Other.Vector2f)
Free()
EndInterface
Prototype Add(Other.Vector2f)
Prototype GetX()
Prototype GetY()
Prototype Free()
Prototype DotProduct(Other.Vector2f)
Structure sVector2fMethod
Add.Add
GetX.GetX
GetY.GetY
DotProduct.DotProduct
Free.Free
EndStructure
Structure sVector2f
*proc.sVector2fMethod ; pointeur sur méthodes
x.f
y.f
EndStructure
Declare Vector2f_Add(*this.sVector2f, Other.Vector2f)
Declare.f Vector2f_GetX(*this.sVector2f)
Declare.f Vector2f_GetY(*this.sVector2f)
Declare.f Vector2f_DotProduct(*this.sVector2f, Other.Vector2f)
Declare Vector2f_Free(*this.sVector2f)
Procedure Vector2f_New(x.f, y.f)
*objet.sVector2f = AllocateStructure(sVector2f)
*objet\proc = AllocateStructure(sVector2fMethod)
*objet\proc\Add = @Vector2f_Add()
*objet\proc\GetX = @Vector2f_GetX()
*objet\proc\GetY = @Vector2f_GetY()
*objet\proc\DotProduct = @Vector2f_DotProduct()
*objet\proc\Free = @Vector2f_Free()
*objet\x = x
*objet\y = y
ProcedureReturn *objet
EndProcedure
Procedure Vector2f_Add(*this.sVector2f, Other.Vector2f)
*otherPtr.sVector2f = Other
*this\x = *this\x + *otherPtr\x
*this\y = *this\y + *otherPtr\y
EndProcedure
Procedure.f Vector2f_GetX(*this.sVector2f)
ProcedureReturn *this\x
EndProcedure
Procedure.f Vector2f_GetY(*this.sVector2f)
ProcedureReturn *this\x
EndProcedure
Procedure.f Vector2f_DotProduct(*this.sVector2f, Other.Vector2f)
*otherPtr.sVector2f = Other
ProcedureReturn (*this\x * *otherPtr\x) + (*this\y * *otherPtr\y)
EndProcedure
Procedure Vector2f_Free(*this.sVector2f)
FreeStructure(*this\proc)
FreeStructure(*this)
EndProcedure
A.Vector2f = Vector2f_New(10,10)
B.Vector2f = Vector2f_New(10,10)
A\Add(B) ; A = A+B
Debug A\GetX() ; A est modifié
Debug B\GetX() ; B ne l'est pas !
Debug A\DotProduct(B)
A\Free()
B\Free()
C'est comme la technique citée plus haut, sauf qu'il n'y a pas de datasection, mais une structure avec des pseudo fonction dedans que j'adresse à la construction de l'objet. cette méthode évite des fonctions du genre : A\add(A,B) mais on écrira plutôt A\Add(B) dans le cadre d'une addition de 2 objets par exemple ( ici des vecteur 2D ) , c'est uniquement l'interface qui donne l'impression de code OO ( l'interface cache le premier paramètre de la fonction ( l'objet en somme...) ) , mais il n'en est rien.
Le code "est lourd" à écrire, mais son utilisation reste agréable et si bien écrit plus intuitif par la suite.