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

前端 未结 7 1656
情歌与酒
情歌与酒 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:27

    It is better to write another method which will be made protected(accessible to the child classes) which will call first the refresh method and then call the function.

    This way the data would be refreshed before the function is called everytime(As per your requirement). eg:

    protected void callFunction1(){
    refresh();
    function();
        }
    

    Thanks, Rajesh

提交回复
热议问题