如何计算类的实例数

Rif*_*ers 6 java variables instances

谁能告诉我如何计算一个类的实例数?

这是我的代码

public class Bicycle {

    //instance variables
    public int gear, speed, seatHeight;
    public String color;

    //constructor
    public Bicycle(int gear, int speed, int seatHeight, String color) {
        gear = 0;
        speed = 0;
        seatHeight = 0;
        color ="Unknown";      
    }

    //getters and setters
    public int getGear() {
        return gear;
    }
    public void setGear(int Gear) {
        this.gear = Gear;
    }

    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int Speed){
        this.speed = Speed;
    }

    public int getSeatHeight() {
        return seatHeight;
    }
    public void setSeatHeight(int SeatHeight) {
        this.seatHeight = SeatHeight;
    }

    public String getColor() {
        return color;
    }
    public void setColor(String Color) {
        this.color = Color;
    }

 }//end class



public class Variable extends Bicycle {

    public Variable(int gear, int speed, int seatHeight, String color) {
        super(gear, speed, seatHeight, color);

    }

}//end class


public class Tester {

    public static void main(String args[]){


       Bicycle bicycle1 = new Bicycle(0, 0, 0, null);
       bicycle1.setColor("red");
       System.out.println("Color: "+bicycle1.getColor());
       bicycle1.setSeatHeight(4);
       System.out.println("Seat Height: "+bicycle1.getSeatHeight());
       bicycle1.setSpeed(10);
       System.out.println("Speed: "+bicycle1.getSpeed());
       bicycle1.setGear(6);
       System.out.println("Gear: "+bicycle1.getGear());

       System.out.println("");//space

       Bicycle bicycle2 = new Bicycle(0, 0, 0, null);
       bicycle2.setColor("black");
       System.out.println("Color: "+bicycle2.getColor());
       bicycle2.setSeatHeight(6);
       System.out.println("Seat Height: "+bicycle2.getSeatHeight());
       bicycle2.setSpeed(12);
       System.out.println("Speed: "+bicycle2.getSpeed());
       bicycle2.setGear(6);
       System.out.println("Gear: "+bicycle2.getGear());

       System.out.println("");//space

    }//end method
 }//end class
Run Code Online (Sandbox Code Playgroud)

类变量用于保持创建的Bicycle类的实例数,测试器类创建Bicycle类的多个实例,并演示Bicycle类和类变量的工作方式.我看了整个互联网,我似乎找不到任何东西,有人可以告诉我该怎么做,谢谢你提前:)

Mar*_*oun 18

由于static变量只初始化一次,并且它们在所有实例之间共享,因此您可以:

class MyClass {

    private static int counter;

    public MyClass() {
        //...
        counter++;
    }

    public static int getNumOfInstances() {
        return counter;
    }
}
Run Code Online (Sandbox Code Playgroud)

阅读有关JLS - 8.3.1.1中static字段的更多信息.静态字段:

如果某个字段声明static,也不管有多少个实例(可能为零)之类的,最终可能创建存在的领域,只有一个化身.在static初始化类时,字段(有时称为类变量)会体现(第12.4节).

请注意,counter隐式设置为零

  • 请注意,此解决方案不是线程安全的。 (3认同)
  • 最好将 `getNumOfInstances()` 设为 `static`。 (2认同)

小智 7

请尝试使用Java工具

jmap -histo <PDID>
Run Code Online (Sandbox Code Playgroud)

放出

     num     #instances         #bytes  class name
----------------------------------------------
   1:       1105141       97252408  java.lang.reflect.Method
   2:       3603562       86485488  java.lang.Double
   3:       1191098       28586352  java.lang.String
   4:        191694       27035744  [C
Run Code Online (Sandbox Code Playgroud)