Java: Is it possible to always execute a certain function before other functions are called? (Like @Before in JUnit)

前端 未结 7 1635
情歌与酒
情歌与酒 2021-01-02 23:59

Is there a way to always execute a function before any other function of a class is called?

I have a class where I need to refresh some fields always before any func

7条回答
  •  既然无缘
    2021-01-03 00:29

    I would define getters for every field and do the refreshment inside the getter. If you want to avoid unrefreshed access to your private fields at all, put them in a superclass (together with the getters which call refresh).

    Depending on your project structure, it may be also sensible to introduce a separate class for all data that is regularly refreshed. It can offer getters and avoid that anyone accesses the non-refreshed fields.

提交回复
热议问题