是否可以使方法只执行一次? [英] Is is possible to make a method execute only once?

查看:136
本文介绍了是否可以使方法只执行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环和这样的结构:

  for(....)
.. ..
....
if(isTrue)
...做点什么..
..要执行一次的方法(doTrick)在for循环之外声明。
.... endif
endfor

public void doTrick()
...
...
..end

for循环中的方法只能执行一次吗?


当然(<已经执行了) {
doTrick();
alreadyExecuted = true;
}


I have a for loop and structure like this:

for(....)
....
....
if(isTrue)
... do something..
.. method to be executed once (doTrick) is declared outside for loop.
....endif
endfor

public void doTrick()
...
...
..end

Is it possible for a method in for loop to be executed only once?

解决方案

Sure!..

if(!alreadyExecuted) {
    doTrick();
    alreadyExecuted = true;
}

这篇关于是否可以使方法只执行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆