我正在尝试使用AspectJ并且在制作计数器时我遇到了一些我不理解的东西。这是代码:
aspect testaAssociacao perthis(execucoesReceita()){
pointcut execucoesReceita() : execution(* Receita.*(..));
int x;
//constructor
public testaAssociacao() { x = 0; }
after(): execucoesReceita(){
x = x+1;
System.out.println("x = "+x);
}
}
在main()中有:
Receita r1 = new Receita("bolo1");
Receita r2 = new Receita("bolo2");
r1.adicionaIngrediente(new Ingrediente("FARINHA", 3));
r1.adicionaIngrediente(new Ingrediente("leite", 1));
r1.adicionaIngrediente(new Ingrediente("FARINHA", 2));
r1.adicionaIngrediente(new Ingrediente("leite", 3));
在这种情况下退出显示:
x = 1
x = 2
x = 1
x = 2
如果我删除了perthis()
声明,因此它不会被对象Receita分开,退出就像这样:
x = 1
x = 2
x = 3
x = 4
x = 5
如果只有4次操作,计数器怎么可能到达5? 它是否考虑main()函数,或者在另一个时刻运行执行?