我正在尝试 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() 函数,还是在另一个时刻运行?