C# 在实例化时递增静态变量
我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为静态变量在类的所有实例之间共享。您想要的是一个静态变量来保存全局计数和一个非静态变量来保存实例化时的当前计数。将上面的代码更改为:
然后在 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:
And then in your ToString() overload you should print myAccountNumber instead of the static variable.
因为它是静态变量。它由该类的所有实例共享。您需要将增加的值保存到实例变量中。
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.
您已将变量 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.
试试这个:
Try this:
那么你可以尝试
一下,你可以得到你想要的。
静态变量在整个运行时只有一份副本。
无论创建该类的实例多少次,该变量都引用相同的内存位置。
you can try
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.
因为静态与类中的所有成员共享?
您需要一个像现在这样可以递增的全局数字的静态变量,但您还需要一个特定于该帐户的私有变量。因此,您应该将以下内容添加
到类定义中,并将构造函数中的现有行修改为:
然后按照您的意愿使用
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:
...to the class definition, and modify your existing line in the constructor to read:
Then use
thisAccountNumber
as you will.