简体   繁体   English

如何判断一个方法是否被调用?

[英]How can I tell if a method is being called?

How can I tell if a method is being called so that I can add a counter to measure the total calls of this method?如何判断一个方法是否被调用,以便我可以添加一个计数器来测量该方法的总调用次数?

Edit for clarification.编辑以澄清。

assuming that I have假设我有

class anything{ 
public String toString() { 
++counter; 
return "the time this method is being called is number " +counter; 
} 
}//end class 

and I am creating an instance of anything in a main method 3 times, and the output I want if I call its toString() the whole 3 times is this:我在 main 方法中创建了 3 次任何东西的实例,如果我把它的 toString() 调用整个 3 次,我想要的输出是这样的:

  • the time this method is being called is number 1调用此方法的时间是 1
  • the time this method is being called is number 2调用此方法的时间是 2
  • the time this method is being called is number 3调用此方法的时间是 3

I want the counter being added succesfully inside the class and inside the ToString() method and not in main.我希望计数器在类中和 ToString() 方法中成功添加,而不是在 main 中。

Thanks in advance.提前致谢。

You can use a private instance variable counter, that you can increment on every call to your method: -您可以使用私有实例变量计数器,您可以在每次调用方法时递增:-

public class Demo {
    private int counter = 0;

    public void counter() {
        ++counter;
    }
}

Update : -更新 : -

According to your edit, you would need a static variable, which is shared among instances.根据您的编辑,您需要一个静态变量,该变量在实例之间共享。 So, once you change that varaible, it would be changed for all instances.因此,一旦您更改了该变量,它就会针对所有实例进行更改。 It is basically bound to class rather than any instance.它基本上绑定到类而不是任何实例。

So, your code should look like this: -所以,你的代码应该是这样的: -

class Anything {   // Your class name should start with uppercase letters.
    private static int counter = 0;

    public String toString() { 
        ++counter; 
        return "the time this method is being called is number " +counter; 
    }
}   

You have 2 options...你有2个选择...

Count the messages for one instance:统计一个实例的消息数:

public class MyClass {
private int counter = 0;

public void counter() {
    counter++;
    // Do your stuff
}

public String getCounts(){
    return "The time the method is being called is number " +counter;
}
}

Or count the global calls for all created instances:或者计算所有创建的实例的全局调用:

public class MyClass {
private static int counter = 0;

public void counter() {
    counter++;
    // Do your stuff
}
public static String getCounts(){
    return "the time the method is being called is number " +counter;
}
}

The best way to do this is by using a private integer field最好的方法是使用私有整数字段

private int X_Counter = 0;

public void X(){
    X_Counter++;
    //Some Stuff
}

It depends on what your purpose is.这取决于你的目的是什么。 If inside you application, you want to use it, then have a counter inside every method to give details.如果在您的应用程序中,您想使用它,那么在每个方法中都有一个计数器来提供详细信息。

But if its an external library, then profilers like VisualVM or JConsole will give you number of invocations of each method.但是如果它是一个外部库,那么像 VisualVM 或 JConsole 这样的分析器会给你每个方法的调用次数。

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

相关问题 我怎么知道什么时候由于方向改变而不是返回/返回而调用onPause()? - How can I tell when onPause() is being called because of orientation change instead of back/home? 如何确保没有调用实例的方法? - How can I make sure no method of an instance is being called? 我可以判断是否调用了抽象方法吗? - Can I tell if an abstract method has been called? 如何获取在grpc-java中调用的rpc方法的注释 - How can I get annotations on rpc method being called in grpc-java 如何从Spring HandlerInterceptor中查找Handler上调用的方法? - How can I lookup the method being called on a Handler from a Spring HandlerInterceptor? 如何防止我的findViewById方法被过早调用? - How can I prevent my findViewById method from being called too early? 如何判断它是对静态方法的调用还是对实例方法的调用? - How can I tell whether it is a call to a static method or an instance method? 如何分辨何时调用接口中的方法 - How to tell when a method in an interface is called 如何检查是否正在调用特定的 class 或 class 的方法? - How do I check if a particular class or a method of a class is being called or not? 类被多次实例化; 如何分辨正在呼叫哪一个? - Class instantiated multiple times; how to tell which one is being called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM