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

C#实例化时递增静态变量,c#,static-variables,C#,Static Variables,我有一个bankAccount对象,我想使用构造函数递增。目标是让它随着类实例化的每个新对象而递增 注意:我已经重写了ToString()以显示accountType和accountNumber 这是我的密码: public class SavingsAccount { private static int accountNumber = 1000; private bool active; private decimal balance; public Sav

我有一个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;
    }
}
为什么我把这个插入main时会这样:

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

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

因为类的所有实例都共享一个静态变量。您需要的是一个静态变量来保存全局计数,一个非静态变量来保存实例化时的当前计数。将上述代码更改为:

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而不是静态变量。

因为静态变量与类中的所有成员共享

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);
    }
}

对于可以递增的全局数,您需要一个静态变量,就像现在一样,但您还需要一个特定于该帐户的专用变量。因此,您应该添加以下内容:

private int thisAccountNumber;
…并将构造函数中的现有行修改为:

thisAccountNumber = accountNumber++;

然后使用
thisAccountNumber

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

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);
    }
}

您已经将变量account声明为static,这意味着它是在类级别而不是实例级别实例化的。所以当你做增量的时候,一个变量会发生两次

实现所需功能的可能方法是在两者之间插入print命令。

尝试以下操作:

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;
    }
}
你可以试试

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

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

您上面的代码不是您正在使用的代码,您可以发布您正在使用的代码吗?即使修复了(请参阅大量正确答案)这是一种非常糟糕的生成帐号的方法-这只是为了某种学术练习吗?请阅读静态变量和实例变量之间的差异,得到这个问题的答案不会有帮助。请注意,我将++移到accountNumber之前,因此它在赋值之前而不是之后递增。预递增与后递增的优点是什么?假设x=100y=x++'将y设置为100,而“y=++x”将y设置为101。它告诉编译器是在赋值之前还是之后递增x。仅在赋值时才考虑预编译和后编译。在上面的答案中。如果他使用了post增量,则在没有增量的情况下,accountNumber的相同值将被分配给myAccountNumber。插入print命令将无法完成他所追求的目标。我知道……这就是为什么我首先解释他所做的是错误的,然后给出了答案。如果他只需要使用两个实例,他可以这样做。即使使用两个实例,这仍然是错误的。两个实例都认为它们具有相同的帐号。我不想在那里存钱:)伙计,我知道……这样想吧:如果你没有储蓄账户,你会怎么做。这基本上是一个黑客,不会让他偏离错误的方向,但会帮助他理解为什么会发生这种情况。这是一个学习引擎盖下正在发生的事情的实际例子:-)啊,是的,是的,在实例化之间打印是获得更多关于正在发生的事情的知识的好方法。很公平。你想要一个静态变量,就像你现在拥有的一样,用于一个可以递增的全局数,但是你也想要一个特定于该帐户的私有变量。因此,您应该将“private int thisAccountNumber”添加到类定义中,并将构造函数中的现有行修改为“thisAccountNumber=accountNumber++”。然后使用“thisAccountNumber”。好吧,“private int thisAccountNumber=0”以确保安全。注意,在.Net类中,任何实例变量都将自动初始化为其默认值(
default(T)
),在本例中,默认值实际上为0。不过,这是一个很好的实践,尤其是在其他不太好的语言中^_^你明白了,我抄了你的答案。因为这是一个如此棘手的问题,我们当然不可能在同一时间打印出相同的解决方案:)我只是给你一个艰难的时刻。而且。。。我必须确保他知道是我先写的,所以我得到了荣誉,哈哈