Methods, classes and interfaces
logiccloud supports the object-oriented extensions of IEC 61131-3: methods, classes, interfaces, inheritance and access control. These build on function blocks.
Methods
Section titled “Methods”A METHOD is a callable, parameterized routine that belongs to a function block
or class. It has its own variable sections and, optionally, a return type.
FUNCTION_BLOCK CounterVAR value : DINT;END_VAR
METHOD Increment VAR_INPUT by : DINT; END_VAR value := value + by; END_METHOD
METHOD Get : DINT Get := value; END_METHOD
END_FUNCTION_BLOCKCall a method on an instance with dot notation:
VAR c : Counter; n : DINT;END_VAR
c.Increment(by := 5);n := c.Get();A method may declare VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, VAR,
VAR_TEMP and VAR_EXTERNAL sections, just like a function.
Classes
Section titled “Classes”A CLASS is a type that bundles data and methods but — unlike a function block
— is not directly assignable to a task. Declare it with CLASS … END_CLASS.
CLASS Vector2DVAR x : REAL; y : REAL;END_VAR
METHOD Length : REAL Length := SQRT(x * x + y * y); END_METHOD
END_CLASSClasses hold VAR and VAR_EXTERNAL sections plus methods.
Interfaces
Section titled “Interfaces”An INTERFACE declares method prototypes (signatures only, no bodies) that
implementing types must provide.
INTERFACE IDrivable METHOD Start END_METHOD
METHOD Stop END_METHOD
METHOD SetSpeed VAR_INPUT rpm : REAL; END_VAR END_METHODEND_INTERFACEAn interface may itself extend one or more other interfaces with EXTENDS.
Inheritance — EXTENDS and IMPLEMENTS
Section titled “Inheritance — EXTENDS and IMPLEMENTS”- A function block can
EXTENDSanother function block (or a class) to inherit its variables and methods. - A class can
EXTENDSanother class. - A function block or class can
IMPLEMENTSone or more interfaces, and must then provide every method those interfaces declare.
FUNCTION_BLOCK Motor IMPLEMENTS IDrivableVAR speed : REAL;END_VAR METHOD Start ... END_METHOD METHOD Stop ... END_METHOD METHOD SetSpeed VAR_INPUT rpm : REAL; END_VAR speed := rpm; END_METHODEND_FUNCTION_BLOCK
FUNCTION_BLOCK ServoMotor EXTENDS Motor // inherits speed, Start, Stop, SetSpeedEND_FUNCTION_BLOCKMethod and type modifiers
Section titled “Method and type modifiers”| Modifier | Where | Meaning |
|---|---|---|
ABSTRACT | CLASS, FUNCTION_BLOCK, METHOD | Cannot be instantiated / has no body; must be overridden by a subtype. |
FINAL | CLASS, FUNCTION_BLOCK, METHOD | Cannot be extended / overridden. |
OVERRIDE | METHOD | Replaces an inherited method of the same signature. |
FUNCTION_BLOCK ABSTRACT Shape METHOD ABSTRACT Area : REAL END_METHODEND_FUNCTION_BLOCK
FUNCTION_BLOCK Circle EXTENDS ShapeVAR radius : REAL;END_VAR METHOD OVERRIDE Area : REAL Area := 3.14159 * radius * radius; END_METHODEND_FUNCTION_BLOCKAccess specifiers
Section titled “Access specifiers”Methods, VAR sections of a class/function block, and class/function-block/
function declarations themselves may carry an access specifier controlling
visibility:
| Specifier | Visibility |
|---|---|
PUBLIC | Accessible from anywhere. |
PROTECTED | Accessible from the declaring type and its subtypes. |
PRIVATE | Accessible only from the declaring type. |
INTERNAL | Accessible within the same namespace. |
FUNCTION_BLOCK AccountVAR PRIVATE balance : REAL;END_VAR
METHOD PUBLIC Deposit VAR_INPUT amount : REAL; END_VAR balance := balance + amount; END_METHOD
END_FUNCTION_BLOCKTHIS and SUPER
Section titled “THIS and SUPER”THISrefers to the current instance — use it to disambiguate a member from a local variable of the same name, or to pass the instance itself.SUPERrefers to the parent type, allowing a subtype to call the overridden implementation it inherited.
FUNCTION_BLOCK ServoMotor EXTENDS Motor METHOD OVERRIDE SetSpeed VAR_INPUT rpm : REAL; END_VAR SUPER.SetSpeed(rpm := rpm); // run the base behaviour first THIS.Calibrate(); // then this instance's extra step END_METHOD
METHOD Calibrate ... END_METHODEND_FUNCTION_BLOCKUse SUPER^ and THIS^ to access them as references where a reference is
required.