您可以重置静态变量吗? [英] Can you reset a static variable?

查看:96
本文介绍了您可以重置静态变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试重置一个静态变量,该静态变量将在运行某些方法时保持计数.返回一种方法的输出后,我希望能够重置计数器. getEfficency可以很好地拉取该值,但是在我运行getEfficency之后,我希望将静态变量重置为0,以便我的程序可以运行其他计算方法.

I have been trying to reset a static variable that will keep count when certain methods are run. I want to be able to reset the counter after I return the output of one of the methods. The getEfficency will pull the value just fine, but after I run the getEfficency I would like for the static variable to be reset to 0, so that my program can run the other compute method.

public class Sequence {

    private static  int efficencyCount;

    public static int computeIterative(int n) {
        efficencyCount++;
    }

    public static int computeRecursive() {
        efficencyCount++;

    }

    public static int getEfficiency() {

        return efficencyCount;
    }
}

推荐答案

只需使用temp变量并将小时静态"设置为0. 另外,您还应该保护静态变量,以免滥用类的变量.

Just use a temp variable and set hour static to 0. Also you should keep protected your static variables to avoid misuse of your variables lutside of your class.

public static int getEfficiency (){
   int temp=efficiencyCount;
   efficiencyCount=0;
   return temp; 
}

这篇关于您可以重置静态变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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