简体   繁体   English

如何为每个对象创建一个数组并将其添加到列表(C#)

[英]How do you create an Array for every Object and add it to a List (c#)

What I'm trying to do is create multiple Objects of the same class, each with their own set of values (eg a name, an address and a personal number) saved in an array, and later add it to a list of employees but when I try to create an Object the name and address stay as null. 我想做的是创建同一类的多个对象,每个对象都有自己的一组值(例如名称,地址和个人编号)保存在数组中,然后将其添加到员工列表中,但是当我尝试创建对象时,名称和地址保持为空。 This is what I have so far: 这是我到目前为止的内容:

 abstract class Person
{
    static public string _name;
    static public string _adresse; 

}

class Student : Person
{
    #region fields

    public string _personalnumber = Fakultät.generate_personalnr();

    #endregion

    //initiating the array witht the Infos about the Student
    public string[] StudentenA = new string[3] { Person._name, Person._address, personalnumber };

    #region const
    public Studenten (string name, string address)
    {
        Person._name = name;
        Person._address = address;

    }
    #endregion

Thanks in advance for your help. 在此先感谢您的帮助。 Bearowl owl

edit 1: changed the variables to all English (all German in original Project) 编辑1:将变量更改为所有英语(原始项目中的所有德语)

Since it looks like a homework, I am not writing out the exact code but try to help you with some hints. 由于它看起来像是一项家庭作业,因此我没有写出确切的代码,而是尝试为您提供一些提示。

The Person class is probably not written properly. Person类可能未正确编写。

  1. If you want each person to have its own name and address, the fields shouldn't be static . 如果您希望每个人都有自己的姓名和地址,则这些字段不应为static

The Student class is probably not written properly. 学生课可能写作不正确。

  1. The person object already contains the name and address field. 人员对象已经包含名称和地址字段。 The student object inherits from it so you just need to add the personal number as another attribute. 学生对象继承自该对象,因此您只需将个人号码添加为另一个属性。 (ie, you probably don't need the array within the Student class) (即,您可能不需要Student类中的数组)
  2. You will need to update your constructor once you have fixed the parent class 修复父类后,您将需要更新构造函数
  3. Once you finish properly coding the Person and Student class, you should then be able to create an array of 3 Student object in your main code 完成对Person和Student类的正确编码后,您应该能够在主代码中创建3个Student对象的数组

Edit: I have a proof of concept (tio.run) fix with examples for class inheritance (method override, etc.). 编辑:我有一个概念证明(tio.run)修复,其中包含类继承示例(方法重写等)。 Please let me know if I should share it here. 请让我知道是否可以在这里分享。 Thanks! 谢谢!

You should assign StudentenA in the constructor, so that it takes place after setting the _name and _address . 您应该在构造函数中分配StudentenA ,以便它在设置_name_address之后_address

public Studenten (string name, string address)
{
    Person._name = name;
    Person._address = address;
    StudentenA = new string[3] { Person._name, Person._address, personalnumber };
}

But also, _name and _address of Person shouldn't be static. 但是,Person的_name_address也不应该是静态的。 With static, only one instance of them exists - not all persons have the same name and address, do they? 使用static时,它们只有一个实例存在-并非所有人的名字和地址都一样,是吗? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM