Setter definition
A setter is an interface that uses the form of assignment. The main purpose is to provide access to the fields.
Table of contents:
Format
The format is:
Access-control setter Setter-name(Argument)
...
endsetter
This is a definition that starts with access control, followed by the setter keyword, and ends with the endsetter keyword.
You can define setters with different argument class even if they have a same name. You can place them anywhere in the class definition.
An assignment to setter is automatically converted to a setter call.
Setter definition will generate a scope.
Access control
You can specify the access level of the setter using the open or closed keywords.
If specifies open, you can call the setter from outside of the class definition.
If specifies closed, the setter can only use inside of the class definition.
setter
This is a keyword that indicates that it is a setter definition. It must be specified next to access control.
Setter name
It specifies the setter name after "setter". The naming rules for setter name is the same as for identifiers.
Exceptionally, you can use the name of built-in class as the setter name.
Argument
Seeter argument is specified next to the Setter-name, it must be enclosed by parentheses.
An argument is a pair of class name and argument name. One argument is required for the setter. The argument class is the class of the value that can be assigned to the setter.
You can define setters with the same name but different argument class. This is because setters are also distinguished in the argument class.
endsetter
This is a keyword that indicates the end of the setter definition.
You can use the abbreviation "es" instead of "endsetter". Note that "es" is also a keyword.
Example
Let's define setters 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 setter Y(int val) |
16: | y = val; |
17: | es // Abbreviation. |
18: | endclass |
The setter is called by an assignment statement.
1: | example ex = new example(); |
2: | ex.X = 10; // open setter X(int val) |
3: | ex.Y = 10; // open setter Y(int val) |