In X++ an interface is a specification for a set of public instance methods. To create an interface, you begin by using the AOT to create a class. You edit the code of itsclassDeclaration node to replace the keyword class with the keyword interface. Then you add methods to the interface just as you would for a class, except no code is allowed inside the method bodies.
The purpose of interfaces is to define and enforce similarities between unrelated classes without having to artificially derive one class from the other.
Implements and extends
You can add the implements keyword on a class declaration, and this requires the class to declare the methods that are specified by the interface. A class declaration can implement multiple interfaces by listing them after the single occurrence of the implements keyword, with commas separating the interface names.
You can add the implements keyword on a class declaration, and this requires the class to declare the methods that are specified by the interface. A class declaration can implement multiple interfaces by listing them after the single occurrence of the implements keyword, with commas separating the interface names.
An interface can extend another interface by using the extends keyword. An interface cannot extend more than one interface.
Public
All interfaces are public regardless of whether you explicitly write the keyword public in front of the keyword interface in the classDeclaration. The methods on an interface are also public, and again the explicit inclusion of the keyword public is optional.
All interfaces are public regardless of whether you explicitly write the keyword public in front of the keyword interface in the classDeclaration. The methods on an interface are also public, and again the explicit inclusion of the keyword public is optional.
All interface methods that a class implements must be declared with the explicit keyword public in the class. Also, a class that implements an interface must also be declared with public.
Related Code Examples
This section shows the code for an imaginary Automobile class that implements two interfaces. The classDeclaration code for the class is shown, as is theclassDeclaration for one of the interfaces. Also, the code for the getSpeed method is shown for the class and the interface.
Automobile Class
Now we examine the code in the Automobile class that implements the IDrivable interface.
classDeclaration for Automobile, include the implements keyword
Finally we examine the full implementation of the getSpeed method on the Automobile class. The method contains X++ in its body between the {}.
getSpeed method implementation
'is' Keyword Works for Interfaces
The following code example demonstrates that the keyword is does work for a class that implements an interface.
static public void Main(Args _args) // X++ { IDrivable yourIDrivable; Automobile myAutomobile; str sTemp = "'is' keyword does Not work for interfaces."; myAutomobile = new Automobile(); if (myAutomobile is IDrivable) { yourIDrivable = myAutomobile; yourIDrivable.setSpeed(42); sTemp = int2str(yourIDrivable.getSpeed()); } Global::info(sTemp); return; } /*** Output to Infolog: Message (06:46:33 pm) 42 ***/
The keyword is also works for a class that extends another class.
No comments:
Post a Comment