Getter definition
A getter is an interface that uses the form of variable reference. The main purpose is to provide access to the fields.
Table of contents:
Format
The format is:
Access-control getter Return-type Getter-name
...
endgetter
This is a definition that starts with access control, followed by the getter keyword, and ends with the endgetter keyword.
You can define getters with different names and place anywhere in the class definition.
The getter definition will generate a scope.
Access control
You can specify the access level of the getter using the open or closed keywords.
If specifies open, you can call the getter from outside of the class definition.
If specifies closed, the getter can only use inside of the class definition.
getter
This is a keyword that indicates that it is a getter definition. It must be specified next to access control.
Return type
It specifies the return type after the getter keyword. This indicates the class of instances returned by the getter.
It uses the return statement to return a value. The class of this value must match the return type.
Getter name
It specifies the getter name after the return type. The naming rules for getter name is the same as for identifiers.
Exceptionally, you can use the name of the built-in class as the getter name.
endgetter
This is a keyword that indicates the end of the getter definition.
You can use the abbreviation "eg" instead of "endgetter". Note that "eg" is also a keyword.
Example
Let's define getters 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: | endclass |
It calls a getter using the form of a variable reference.
1: | example ex = new example(10, 10); |
2: | int cx = ex.X; // open getter int X |
3: | int cy = ex.Y; // open getter int Y |