简体   繁体   English

满足条件时获取时间变量的实例

[英]Taking an Instance of A Time Variable When A Condition Is Met

I am looking for a way to take an instance of a time variable when the else of an if is called as in the situation below, so then I can pass this time taken into another method. 我正在寻找一种在以下情况中调用if的else时采用时间变量的实例的方法,因此我可以将此时间传递给另一个方法。

double time = getRunningTime();
...
if (..){

}
else{
  *TAKE AN INSTANCE OF VARIABLE TIME AT THIS POINT TO PASS TO ANOTHER
                           METHOD*
}

However, if I now pass this into another method by 但是,如果我现在通过以下方式将其传递给另一种方法

else{
  methodToCall(time)
}

Then use this in the other method ie 然后在其他方法中使用它

methodToCall(double time){
   double timeMinusRunningTimeOfIfInPreviousMethod = getRunningTime() - time
   ....
}

the variable time will still be changing, however I want it to stay constant. 可变时间仍然会改变,但是我希望它保持不变。

Thanks, Jack 谢谢杰克

From your comment, 根据您的评论,

Precisely. 精确地 If i do that now the variable will still constantly change. 如果我现在这样做,变量将仍然不断变化。

No, time will not change if you do that now. 不,如果您现在这样做, time不会改变。 In fact, time never changes after the first line in your snippet. 实际上,在代码段的第一行之后, time永远不会改变。 time is a value type of double . timedouble的值类型。 It does not hold a reference to some constantly changing object. 它没有引用某些不断变化的对象。

So what is constantly changing here? 那么,这里不断变化的是什么? Presumably, the return value of getRunningTime . 大概是getRunningTime的返回值。 When you call the method at a different time, a different value will be returned. 当您在其他时间调用该方法时,将返回不同的值。 Judging from the name, I guess it returns how long has the program been running since some point in time. 从名称来看,我猜它返回程序从某个时间点开始运行了多长时间。

Therefore, you probably want to do something like this instead: 因此,您可能想要改为执行以下操作:

someMethod(getRunningTime());

Or if getRunningTime returns the current time and you want to measure how long the if statement took to execute, you can do: 或者,如果getRunningTime返回当前时间,并且您想测量if语句执行的时间,则可以执行以下操作:

someMethod(getRunningTime() - time).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM