The following example creates an IDL object class with both instance and static methods.
The ObjCircle class inherits from the IDL_Object superclass. To run the example, save the following code into a file called "objcircle__define.pro" somewhere on IDL's path.
FUNCTION ObjCircle::Init, _EXTRA=ex
COMPILE_OPT IDL2
void = self->IDL_Object::Init()
IF (ISA(ex)) THEN self->SetProperty, _EXTRA=ex
RETURN, 1
END
PRO ObjCircle::Cleanup
COMPILE_OPT IDL2
self->IDL_Object::Cleanup
END
FUNCTION ObjCircle::Area
COMPILE_OPT IDL2
RETURN, !const.pi*self.radius^2
END
FUNCTION ObjCircle::GetCircles
COMPILE_OPT IDL2, static
obj = OBJ_VALID(COUNT=c)
RETURN, obj[WHERE(OBJ_ISA(obj, 'ObjCircle'), /NULL)]
END
pro ObjCircle::Plot, _EXTRA=ex
compile_opt idl2
w = WINDOW()
p = ELLIPSE(self.center[0], self.center[1], $
MAJOR=self.radius, _EXTRA=ex)
end
PRO ObjCircle::GetProperty, $
CENTER=center, PI=pi, RADIUS=radius
COMPILE_OPT IDL2, static
IF (ISA(self)) THEN BEGIN
IF (ARG_PRESENT(center)) THEN center = self.center
IF (ARG_PRESENT(radius)) THEN radius = self.radius
endif else begin
IF (ARG_PRESENT(pi)) THEN pi = !const.pi
ENDELSE
END
PRO ObjCircle::SetProperty, CENTER=center, RADIUS=radius
COMPILE_OPT IDL2
IF (ISA(center)) THEN self.center = center
IF (ISA(radius)) THEN self.radius = radius
END
PRO ObjCircle__define
COMPILE_OPT IDL2
void = {ObjCircle, $
inherits IDL_Object, $
center: [0d, 0d], $
radius: 0d}
END
After the above code is saved, you can then execute the following lines of code to try out your new ObjCircle class:
COMPILE_OPT IDL2
a = ObjCircle()
a.center = [50, 60]
a.radius = 20
PRINT, a.center
b = ObjCircle(CENTER=[100,200], RADIUS=40)
PRINT, b.Area()
obj = ObjCircle.GetCircles()
PRINT, obj
PRINT, ObjCircle.Pi
a = 0
b = 0
obj = 0
Note that in all of the methods and procedures above, we used compile_opt idl2 to avoid any confusion between the use of parentheses for array indexing versus function calls. See Static Methods for more details on the use of compile_opt.