计算已实例化某个类的对象的次数

时间:2012-05-20 18:46:42

标签: c# class xna counter

我有一个名为Button的类,它创建了要在屏幕上显示的按钮。我想为每个按钮创建一个信息数组,但需要知道已创建了多少个按钮。有没有一种简单的方法可以跟踪一个类被实例化的次数?我试过这个并且它似乎不起作用...我想如果你把计数器放在构造函数中它会每次添加一个,但它似乎不起作用。

private int children = 0;

public Button(Vector2 position, Vector2 fontPos, Color buttonColor, 
              String buttonText, Boolean clickable, String spriteName)
{
    this.position = position;
    this.buttonColor = buttonColor;
    this.buttonText = buttonText;
    this.clickable = clickable;
    this.spriteName += spriteName;
    this.fontPos = fontPos;
    children++;
}

1 个答案:

答案 0 :(得分:8)

让它静止

private static int children = 0;
相关问题