Methods
Methods are functions that a class can have. This adds functionality to a class and provides the behaviour for each method. The library has a FunctionSpec
which allows the creation of functions that can be added to a class.
Instance methods
A class definition can have an unspecific amount of instance methods. An instance method can access instance variables from a class and use them for his behaviour.
For our example we have a small two-dimensional Point
and want to add the behaviour that a function calculates the distance to another point object.
Point class:
Now we create our FunctionSpec which creates the required method:
The generated code for this function is the following:
And the generated file looks like this (for this example we say that we have already a FileSpec for this case:
Operators
Getters and setters
Getters and setters are a special type of methods. Those methods provide read and write access to an object's properties. Dart generates an implicit getter, plus a setter if appropriate, for every instance variable of a class. You can create additional properties by implementing getters and setters, using the FunctionSpec
with the right attributes.
The creation of getters and setters is very similar to the creation of a normal function. To tell the library that it should be as setter, you need to set the setter
or getter
attribute to true
.
Getter example
The getter will be generated as the following:
Setter example
Abstract methods
Functions, getters and setters can be abstract. Abstraction is more relevant when the functions etc. are defined in an interface. In this case the implementation is delegated to another class which relies on the interface.