C#在实例化时递增静态变量

时间:2011-11-15 06:37:08

标签: c# static-variables

我有一个bankAccount对象,我想使用构造函数递增。目标是让它与类实例化的每个新对象一起递增。

注意:我已经覆盖了ToString()以显示accountType和accountNumber;

这是我的代码:

public class SavingsAccount
{
    private static int accountNumber = 1000;
    private bool active;
    private decimal balance;

    public SavingsAccount(bool active, decimal balance, string accountType)
    {
        accountNumber++;
        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }
}

为什么当我把它插入主体时是这样的:

class Program
{
    static void Main(string[] args)
    {
        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(potato.ToString());
        Console.WriteLine(magician.ToString());
    }
}

我得到的输出并没有单独增加,即

savings 1001
savings 1002

但我得到了:

savings 1002
savings 1002

我如何让它成为前者而不是后者?

6 个答案:

答案 0 :(得分:7)

因为静态变量在类的所有实例之间共享。你想要的是一个静态变量来保持全局计数和一个非静态变量来保存实例化时的当前计数。将上面的代码更改为:

public class SavingsAccount
{
    private static int accountNumber = 1000;
    private bool active;
    private decimal balance;
    private int myAccountNumber;

    public SavingsAccount(bool active, decimal balance, string accountType)
    {
        myAccountNumber = ++accountNumber;
        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }
}

class Program
{
    static void Main(string[] args)
    {
        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(potato.ToString());
        Console.WriteLine(magician.ToString());
    }
}

然后在你的ToString()重载中你应该打印myAccountNumber而不是静态变量。

答案 1 :(得分:3)

因为它是一个静态变量。它由类的所有实例共享。您需要将递增的值保存到实例变量。

public class SavingsAccount
{
    private static int accountNumberCounter = 1000;
    private int accountNumber;
    private bool active;
    private decimal balance;

    public BankAccount(bool active, decimal balance, string accountType)
    {
        accountNumberCounter++;
        this.accountNumber = accountNumberCounter;

        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }

    public string ToString() 
    {
        return String.Format("{0} {1}", accountType, accountNumber);
    }
}

答案 2 :(得分:0)

因为静态是与班级中的所有成员共享的吗?

您想要一个静态变量,就像您现在拥有的可以递增的全局数字一样,但您还需要一个特定于该帐户的私有变量。因此,你应该添加:

private int thisAccountNumber;

...到类定义,并将构造函数中的现有行修改为:

thisAccountNumber = accountNumber++;

然后使用thisAccountNumber

答案 3 :(得分:0)

您已将变量帐户声明为静态,这意味着它在类级别而非实例级别进行实例化。因此,当您执行增量时,对于一个变量,它会发生两次。

实现目标的可能方法是在两者之间插入打印命令。

答案 4 :(得分:0)

试试这个:

public class SavingsAccount
{
    private static int accountNumberMarker = 1000;
    private int accountNumber;
    private bool active;
    private decimal balance;

    public SavingsAccount(bool active, decimal balance, string accountType)
    {
        accountNumber = ++accountNumberMarker;
        this.active = active;
        this.balance = balance;
        this.accountType = accountType;
    }
}

答案 5 :(得分:0)

你可以尝试

        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        Console.WriteLine(potato.ToString());
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(magician.ToString());
那么,你可以得到你想要的。

静态变量在整个运行时中只有一个副本。 无论创建类的实例多少次,变量都指向相同的内存位置。