In one sentence
A method is a subprogram that lives inside a class and works on a particular object: the one named before the dot.
9.2.1 How to write it⟦ Public | Private ⟧ Method «nombre» ( «parámetros» ) ⟦ As «tipo» ⟧
«instrucciones»
EndMethod Parts
Method opens the method. It may be preceded by Public (the default) or Private .«parameters» and As «type» follow the same rules as in a subprogram .EndMethod closes the method.9.2.2 How it worksA method is called on an object: pasos . Sumar ( ) . That object is the receiver , and inside the method it is named with This . To use an attribute or method of the current object, This . is required : a bare name can only be a local variable or a parameter.
Program A counter with state Class Contador
Private Define valor As Integer ;
Method Constructor ( inicial As Integer )
This . valor <- inicial ;
EndMethod
Method Sumar ( )
This . valor <- This . valor + 1 ;
EndMethod
Method Actual ( ) As Integer
Return This . valor ;
EndMethod
EndClass
Algorithm Pasos
Define pasos As Contador <- New Contador ( 10 ) ;
pasos . Sumar ( ) ;
pasos . Sumar ( ) ;
Write pasos . Actual ( ) ;
EndAlgorithm The program finishes normally.
Figure 9.2-1. When calling pasos . Sumar ( ) , This inside the method is the same object as pasos .The variable pasos and, inside the method, This: both are references pointing to the same object, with valor = 12.
The dot can be chained: New Contador ( 1 ) . Actual ( ) . An attribute after the dot is a designator ; a method call is not. The receiver is computed only once.
There is no overloading: two members of a class cannot share a name, even if one is an attribute and the other a method.
Under the hood
A method is one more function of the intermediate representation with a hidden first parameter: the receiver. pasos . Sumar ( ) becomes a call that passes pasos as that first argument.
Intermediate representation
module Pasos en
class Contador { valor: i64 }
func suspendible @main() -> unit {
b0() mass 4:
stmt.enter @18:3-18:47
%0 = const.i64 10 @18:44-18:46
%1 = object.new Contador @18:31-18:47
%2 = call @Contador.Constructor(%1, %0) @18:31-18:47
stmt.exit @18:3-18:47
stmt.enter @19:3-19:16
%3 = call @Contador.Sumar(%1) @19:3-19:16
stmt.exit @19:3-19:16
stmt.enter @20:3-20:16
%4 = call @Contador.Sumar(%1) @20:3-20:16
stmt.exit @20:3-20:16
stmt.enter @21:3-21:23
%5 = call @Contador.Actual(%1) @21:9-21:23
out.write %5, newline @21:3-21:23
stmt.exit @21:3-21:23
return @22:1-22:13
}
func @Contador.Constructor(%0: obj<Contador>, %1: i64) -> unit {
b0() mass 1:
stmt.enter @5:5-5:26
object.store %0, valor, %1 @5:5-5:26
stmt.exit @5:5-5:26
return @6:3-6:12
}
func @Contador.Sumar(%0: obj<Contador>) -> unit {
b0() mass 1:
stmt.enter @9:5-9:33
%1 = object.load %0, valor @9:19-9:30
%2 = const.i64 1 @9:32-9:33
%3 = int.add %1, %2 @9:30-9:31
object.store %0, valor, %3 @9:5-9:33
stmt.exit @9:5-9:33
return @10:3-10:12
}
func @Contador.Actual(%0: obj<Contador>) -> i64 {
b0() mass 1:
stmt.enter @13:5-13:22
%1 = object.load %0, valor @13:12-13:22
stmt.exit @13:5-13:22
return %1 @13:5-13:22
} In short
A method works on its receiver, the object before the dot. Inside, This . is required for the object's members. Methods can be public or private; there is no overloading. See also