[英]How to assign different values of variables to different instances of the same object?
I'm trying to set up a very basic system that creates a Levels object via its constructor: 我正在尝试建立一个非常基本的系统,该系统通过其构造函数创建一个Levels对象:
class Levels
{
// private level properties
private static string levelName { get; set; }
private static int levelSize { get; set; }
private static int levelNum { get; set; }
// new level constructor. Takes name, size (to square) and level number.
public Levels(string inName, int inSize, int inNum)
{
levelName = inName;
levelSize = inSize;
levelNum = inNum;
}
// ... GetLevel methods here...
}
and assigns those values to each particular instance of the object. 并将这些值分配给对象的每个特定实例。 However the output indicates to me that the levelName, levelSize and levelNum variables aren't "sticking" to each instance properly.
但是输出告诉我,levelName,levelSize和levelNum变量未正确“粘贴”到每个实例。
class Program
{
static void Main(string[] args)
{
var Level1 = new Levels("Forest", 15, 1);
var Level2 = new Levels("Desert", 22, 2);
Console.WriteLine($"--- {Level1.GetLevelName()}, level {Level1.GetLevelNum()} ---");
Console.WriteLine($"--- {Level2.GetLevelName()}, level {Level2.GetLevelNum()} ---");
Console.ReadLine();
}
}
// output:
// --- Desert, level 2 ---
// --- Desert, level 2 ---
// why is level 1 not displaying here?
I'm aware that if I were to move the constructor for level 2 after the first WriteLine command it would, hamfistedly, work, but obviously I need those values to stick to their instance. 我知道,如果我要在第一个WriteLine命令之后将级别2的构造函数移走,那实际上是可行的,但是显然我需要这些值坚持其实例。
The keyword static
means that the variable is not instance level, but class level. 关键字
static
表示变量不是实例级别,而是类级别。 Meaning your variables will be shared among all instances of your Levels
class. 这意味着您的变量将在
Levels
类的所有实例之间共享。
I would suggest rewriting it to: 我建议将其重写为:
public class Levels
{
public string LevelName { get; private set; }
public int LevelSize { get; private set; }
public int LevelNum { get; private set; }
// new level constructor. Takes name, size (to square) and level number.
public Levels(string inName, int inSize, int inNum)
{
LevelName = inName;
LevelSize = inSize;
LevelNum = inNum;
}
}
Instead of your variables being private static
, you will now use Properties that have a private setter (meaning they can only be set inside the class). 现在,您将使用具有私有设置器的Properties (这意味着它们只能在类内部设置),而不是使用变量为
private static
。 Now each of your instances of Levels
has their own respective values and nothing is shared between instances. 现在,您的每个
Levels
实例都有各自的值,实例之间没有任何共享。 This will also give you the added bonus of eliminating the need for your GetLevelName()
, GetLevelNum()
, etc methods too. 这也将给您带来额外的好处,那就是消除了对
GetLevelName()
, GetLevelNum()
等方法的需要。 You can now just say: 您现在可以说:
Console.WriteLine($"--- {Level1.LevelName}, level {Level1.LevelNum} ---");
Remove static
keyword from fields. 从字段中删除
static
关键字。 Basically static
keyword means that this field is the same for all instances of this class. 基本上,
static
关键字表示该字段对于此类的所有实例都是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.