Declaration of an interface incl. method prototypes
INTERFACE name_1 ( * optional_begin *) EXTENDS interface_2, interface_3, ... interface_n (* optional_end *)
(* optional: declarations of method prototypes *)
END_INTERFACE
Meaning |
declaration of an →interface
(a feature of the →object-oriented programming)
, name_1 must be an →IEC-identifier. In the object oriented programming, the interface separates the interface specification from its implementation as a →class. This allows different implementations of a common interface specification.
The keyword EXTENDS
is optional. Specify EXTENDS, if you want to derive the interface from other interfaces (also known as base interfaces). Specify the name of the base interfaces after EXTENDS (separated by a comma). |
You are able to declare only →method prototypes within an interface. See the following section "Declaration of a method prototype".
An interface may be used as the type of a variable (see "Declaration of variables based on an interface"). Moreover, a function block can implement the interfaces (see " Declaration of a function block in ST ").
An interface behaves as a →function block does.
Declaration of a method prototype
A method prototype is a restricted declaration of a →method (with the implicit modifier PUBLIC ) for the use with an interface.
METHOD name_1 (* optional_begin *) : type (* optional_end *)
(* optional: declaration of variables but only input variables, output variables and in-out variables are allowed in method prototypes *)
END_METHOD
In contrast to a method (see "Declaration of a method" for details), a method prototype may contain these elements only:
the name of the method prototype
optional: the data type of the method prototype (for a return value)
optional: the declaration of →input variables, →output variables and →in-/out variables
All other variables (e.g. →temporary variables ) must not be declared in a method prototype.
Observe that no keywords (e.g. modifiers) and no body (hence, a code to execute) are defined in a method prototype as this is possible for a method.
INTERFACE COUNT
METHOD UP : UINT
VAR_INPUT
INC: UINT;
END_VAR
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
METHOD UP5 : UINT
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
INTERFACE COUNT_Original
(* "COUNT_Original" contains the method prototypes "UP" und "UP5". *)
METHOD UP : UINT
VAR_INPUT
INC: UINT;
END_VAR
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
METHOD UP5 : UINT
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
INTERFACE COUNT_BASE
(* "COUNT_BASE" contains the method prototype "UP". *)
METHOD UP : UINT
VAR_INPUT
INC: UINT;
END_VAR
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
INTERFACE COUNT EXTENDS COUNT_BASE
(* "COUNT" contains the method prototype "UP5". Due to "EXTENDS COUNT_BASE", there is also the method prototype "UP". *)
METHOD UP5 : UINT
VAR_OUTPUT
QU: BOOL;
END_VAR
END_METHOD
END_INTERFACE
INTERFACE COUNT3 EXTENDS COUNT_BASE, COUNT_Original
(* "COUNT3" contains the method prototype "UP10". Due to "EXTENDS COUNT_BASE, COUNT_Original", there are also the method prototypes "UP5" und "UP". *)
(* Here an error is reported. Reason: "UP" exists multiple times in the different interfaces "COUNT_BASE" und "COUNT_Original". *)
METHOD UP10 : UINT
END_METHOD
END_INTERFACE
INTERFACE Sample EXTENDS COUNT_BASE, COUNT_BASE
(* Due to "EXTENDS COUNT_BASE, COUNT_Original", there is also the method prototype "UP". *)
(* This is OK although "UP" actually exists multiple times. The 2nd occurrence of "UP" in "COUNT_BASE" is ignored because "UP" is derived from the same interface "COUNT_BASE". *)
END_INTERFACE
INTERFACE COUNT2 EXTENDS COUNT_BASE, COUNT
(* "COUNT2" contains the method prototype "UP10". Due to "EXTENDS COUNT_BASE, COUN", there are also the method prototypes "UP5" und "UP". *)
(* This is OK although "UP" actually exists multiple times. The 2nd occurrence of "UP" in "COUNT" is ignored because "UP" is derived from the same interface "COUNT_BASE". Background: The interface "COUNT" is derived from the interface "COUNT_BASE" as well. *)
METHOD UP10 : UINT
END_METHOD
END_INTERFACE
INTERFACE I_Motor
/*
* Returns the current speed of the motor in RPM.
*/
METHOD GetCurrentSpeed : UINT
END_METHOD
/*
* Sets the desired speed of the motor.
* The desired ramp-up time can be specified by an input parameter.
* Returns the current motor speed as an output parameter.
*/
METHOD SetSpeed
VAR_INPUT
i_RampUpTime : TIME := T#1s;
i_DesiredSpeed : UINT;
END_VAR
VAR_OUTPUT
o_CurrentSpeed : UINT;
END_VAR
END_METHOD
END_INTERFACE
More examples for interfaces are specified under " Examples on using interfaces and variables based on these interfaces (incl. assignments) ".