Method definition
A method is an interface that uses the form of method call. The main purpose is to specify behaviar of the class.
Table of contents:
Format
The format is:
Access-control method Return-type Method-name(Argument)
...
endmethod
This is a definition that starts with access control, followed by the method keyword, and ends with the endmethod keyword.
You can define methods with different order of arguments even if they have a same name. You can place them anywhere in the class definition.
The method definition will generate a scope.
Access control
You can specify the access level of the method using the open or closed keywords.
If specifies open, you can call the method from outside of the class definition.
If specifies closed, the method can only use inside of the class definition.
method
This is a keyword that indicates that it is a method definition. It must be specified next to access control.
Return type
It specifies the return type after the method keyword. This indicates the class of instances returned by the method.
You should specify void if the method does not return a value.
If the return type is not the void class, it uses the return statement to return a value. The class of this value must match the return type.
The return statement is optional if it does not return a value.
Method name
It specifies the method name after the return type. The naming rules for method name is the same as for identifiers.
Exceptionally, you can use the name of the built-in class as the method name.
Argument
Method arguments are specified next to the Method-name, it must be enclosed by parentheses.
An argument is a pair of class name and argument name.
If there are two or more arguments, separate them with commas. No comma is required when one argument. If no argument, the parentheses are empty.
You can define methods with different order of arguments even if they have a same name. This is because the method are distinguished by the order of the argument classes.
endmethod
This is a keyword that indicates the end of the method definition.
You can use the abbreviation "em" instead of "endmethod". Note that "em" is also a keyword.
Example
Let's define methods in the example class.
1: | class example |
2: | int x; |
3: | int y; |
4: | open fitter example() // Default fitter |
5: | x = 0; |
6: | y = 0; |
7: | endfitter |
8: | open fitter example(int xx, int yy) // Fitter with two arguments. |
9: | x = xx; |
10: | y = yy; |
11: | ef // Abbreviation. |
12: | open setter X(int val) |
13: | x = val; |
14: | endsetter |
15: | open getter int X |
16: | return x; |
17: | endgetter |
18: | open setter Y(int val) |
19: | y = val; |
20: | es // Abbreviation. |
21: | open getter int Y |
22: | return y; |
23: | eg // Abbreviation. |
24: | open method void Clear() |
25: | x = 0; |
26: | y = 0; |
27: | return; // Optional, not required. |
28: | endmethod |
29: | open method example Add(int vx, int vy) |
30: | return new example(x + vx, y + vy); |
31: | endmethod |
32: | open method example Add(example ve) // Same name, different argument. |
33: | return new example(x + ve.x, y + ve.y); |
34: | em // Abbreviation. |
35: | endclass |
It calls the method using a method call expression.
1: | example ex = new example(10, 10); |
2: | ex.Clear(); |
3: | example ex2 = ex.Add(10, 10); |
3: | example ex3 = ex.Add(ex2); |