统计程序中目前存在多少个该类的对象

import java.util.Scanner;


class Objectcounter{
    static int cnt;
    Objectcounter(){
        cnt++;
        //System.out.println(cnt);
    }
    public static int getCounter(){
        return cnt;
    }

}

public class Main {
    public static void main(String [] args){
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        for(int i=0;i<n;i++)
        {
            Objectcounter str1=new Objectcounter();
        }
        System.out.println(Objectcounter.getCounter());
    }
}
0

Leave a Reply

Your email address will not be published.