invocationcounter.java

来自「java编程代码」· Java 代码 · 共 44 行

JAVA
44
字号

public class InvocationCounter
{
    private static int numberOfInvocations = 0;

    public void demoMethod( )
    {
        numberOfInvocations++;
        //In a real example, more code would go here.
    }
   
    public void outPutCount( )
    {
        numberOfInvocations++;
        System.out.println("Number of invocations so far = "
                                      + numberOfInvocations);
    }

    public static int numberSoFar( )
    {
        numberOfInvocations++;
        return numberOfInvocations;
    }

    public static void main(String[] args)
    {
        int i;
        InvocationCounter object1 = new InvocationCounter( );
        for (i = 1; i <= 5 ; i++)
           object1.demoMethod( );
        object1.outPutCount( );

        InvocationCounter object2 = new InvocationCounter( );
        for (i = 1; i <= 5 ; i++)
        {
           object2.demoMethod( );
           object2.outPutCount( );
        }

        System.out.println("Total number of invocations = "
                           + numberSoFar( ));
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?