【问题标题】:Count the number of times objects of a certain class have been instantiated计算某个类的对象被实例化的次数
【发布时间】:2012-05-20 18:46:42
【问题描述】:

我有一个名为 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++;
}

【问题讨论】:

标签: c# class xna counter


【解决方案1】:

静态化

private static int children = 0;

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 2023-04-04
    • 1970-01-01
    • 2012-03-24
    • 2013-03-28
    • 2021-04-17
    • 2012-09-04
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多