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

发布于 2024-12-15 07:15:51 字数 1048 浏览 3 评论 0原文

我有一个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

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

I have a bankAccount object I'd like to increment using the constructor. The objective is to have it increment with every new object the class instantiates.

Note: I've overriden the ToString() to display the accountType and accountNumber;

Here is my code:

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

Why is it that when I plug this in main like so:

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

The output I get does not increment it individually i.e.

savings 1001
savings 1002

but instead I get:

savings 1002
savings 1002

How do I make it to be the former and not the latter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅忆 2024-12-22 07:15:51

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

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

Because a static variable is shared among all instances of the class. What you want is a static variable to keep the global count and a non-static variable to save the current count at the time of instantiation. Change your code above to:

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

And then in your ToString() overload you should print myAccountNumber instead of the static variable.

狼性发作 2024-12-22 07:15:51

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

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

Because it is a static variable. It is shared by all instances of the class. You need to save off the incremented value to an instance variable.

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);
    }
}
失眠症患者 2024-12-22 07:15:51

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

实现您想要的效果的可能方法是在两者之间插入打印命令。

You have declared the variable account as static which means its instantiated at class level and not instance level. So when you do the increment it happens two times for when one variable.

The possible way to achieve what you want is to insert the print command between the two.

差↓一点笑了 2024-12-22 07:15:51

试试这个:

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

Try this:

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;
    }
}
七度光 2024-12-22 07:15:51

那么你可以尝试

        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        Console.WriteLine(potato.ToString());
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(magician.ToString());

一下,你可以得到你想要的。

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

you can try

        SavingsAccount potato = new SavingsAccount(true, 100.0m, "Savings");
        Console.WriteLine(potato.ToString());
        SavingsAccount magician = new SavingsAccount(true, 200.0m, "Savings");
        Console.WriteLine(magician.ToString());

then , you could get you want.

The static variable has only one copy in entire runtime.
No matter how many times created the class's instance , the variable is referring to the same memory location.

允世 2024-12-22 07:15:51

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

您需要一个像现在这样可以递增的全局数字的静态变量,但您还需要一个特定于该帐户的私有变量。因此,您应该将以下内容添加

private int thisAccountNumber;

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

thisAccountNumber = accountNumber++;

然后按照您的意愿使用 thisAccountNumber

Because a static is shared with all members in the class?

You want a static variable like you have now for a global number that you can increment, but you also want a private variable specific to that account. Hence, you should add this:

private int thisAccountNumber;

...to the class definition, and modify your existing line in the constructor to read:

thisAccountNumber = accountNumber++;

Then use thisAccountNumber as you will.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文