Enjoy:
Code: Select all
; Simple Purebasic OOP example
; Format for classes:
; PROTOTYPES - Only necessary for methods not in parent classes. Declared first so that the structure can read it.
; STRUCTURE - Variables, then methods, order doesnt really matter. - METHODNAME.PROTOTYPE
; METHODS - Methods with 'CLASSNAME_' before them to distinguish between them and procedures
; CONSTRUCTOR - At the bottom so it can set all of the methods appropriately
;
;{ Class Rectangle
Prototype Rectangle_size(*this, width.i, height.i);
Prototype Rectangle_move(*this, x.i, y.i);
Structure Rectangle;
x.i;
y.i;
width.i;
height.i;
size.Rectangle_size;
move.Rectangle_move;
EndStructure;
Procedure Rectangle_size(*this.Rectangle, width.i, height.i);
*this\width = width;
*this\height = height;
EndProcedure;
Procedure Rectangle_move(*this, x.i, y.i)
Debug "moving";
EndProcedure;
; Constructor
Procedure Rectangle_(*this.Rectangle);
*this\size = @Rectangle_size();
*this\move = @Rectangle_move();
EndProcedure;
;}
;{ Class ColouredRectangle
Structure ColouredRectangle Extends Rectangle;
colour.l;
EndStructure;
; Method override for size
Procedure ColouredRectangle_size(*this.ColouredRectangle, width.i, height.i);
*this\width = width;
*this\height = height;
Debug "Coloured sizing instead!";
EndProcedure;
; Constructor
Procedure ColouredRectangle_(*this.ColouredRectangle);
Rectangle_(*this);
*this\size = @ColouredRectangle_size();
EndProcedure;
;}
; Rectangle myRect = new Rectangle()
Rectangle_(myRect.Rectangle);
myRect\size(myRect, 20,20);
Debug myRect\width;
; ColouredRectangle mycRect = new ColouredRectangle()
ColouredRectangle_(mycRect.ColouredRectangle);
mycRect\colour = RGB(20,12,20);
mycRect\size(mycRect,22,91);
Debug mycRect\colour;