简体   繁体   English

如何将变量的不同值分配给同一对象的不同实例?

[英]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} ---");

I made a fiddle here to demo. 我在这里摆弄小提琴进行演示。

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.

相关问题 WPF中同一用户控件的不同实例中的不同值 - Different values in different instances of the same usercontrol in WPF 如何隔离同一组件的不同实例之间的js变量? - How to isolate js variables between different instances of the same component? Entity Framework Core:如何将不同的对象实例映射到同一个实体? - Entity Framework Core: how to map different object instances to the same entity? 遍历数据表并将值分配给不同的变量 - Iterate over datatable and assign values to different variables 如何使用对象初始化器将相同的值分配给不同的属性 - How to assign a same value to different properties using Object Initializer 如何分配在2个不同名称空间中声明的相同类对象 - How to assign same class object declared in 2 different namespaces 我们可以注入具有不同变量的相同 class 的多个实例吗? - Can we inject multiple instances of the same class with different variables? 如何将同一事件分配给不同的对象? - how to assign same event to different objects? 如何将不同的泛型类分配给同一变量 - How to assign different generic classes to same variable 为 UserControl 的不同实例传递变量 - Passing in variables for different instances of UserControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM