简体   繁体   English

从具有只读值的抽象 class 调用构造函数

[英]Call Constructor from abstract class with readonly values

Im trying to call this constructor from Student into the collegestudent.我试图将这个构造函数从 Student 调用到大学生中。

Here is the constructor:这是构造函数:

abstract class Student
{
    public readonly string FirstName;
    public readonly string LastName;
    public readonly string StudentID;

    public Student(string Value)
    {
        FirstName = Value;
        LastName = Value;
        StudentID = Value;
    }

    public string Value
    {
        get { return FirstName + LastName + StudentID; }
    }
}

Here is CollegeStudent:这是大学生:

class CollegeStudent : Student, IMathClass
{
    public Student(string Tim, string russell, string studentid)
    {
        FirstName = Tim;
        LastName = russell;
        studentID = studentid;
    }
}

Im getting following errors我收到以下错误

  • "Method must have a return type" “方法必须有返回类型”
  • "A readonly field cannot be assigned to" “无法将只读字段分配给”

The problem might be the initialization order.问题可能是初始化顺序。 A readonly variable can only be assigned once (within the constructor when it's a class variable). readonly变量只能分配一次(在构造函数中,当它是 class 变量时)。 When you create the object, the variable will already be initialized by the Student constructor because this will be called first (before the constructor of CollegeStudent ).当您创建 object 时,该变量将已由Student构造函数初始化,因为它将首先调用(在CollegeStudent的构造函数之前)。 When you try to initialize it (again) in the CollegeStudent constructor, it's already initialized and therefore an error raise.当您尝试(再次)在CollegeStudent构造函数中初始化它时,它已经初始化,因此会引发错误。

Furthermore, I'd suggest some changes:此外,我建议进行一些更改:

First: Make the properties not readonly but define only a public getter and private/protected setter.首先:使属性不是readonly的,而是只定义一个公共 getter 和私有/受保护的 setter。 This might already solve your problem.这可能已经解决了您的问题。

public string Firstname {get; protected set;}

Second: The next thing is the constructor and the parameter names of your CollegeStundet .第二:接下来是CollegeStundet的构造函数和参数名称。 As these are parameters (variables containing information) they look really strange.由于这些是参数(包含信息的变量),它们看起来很奇怪。 Better would be:更好的是:

public CollegeStudent(string firstName, string lastName, string studentId) 

You will use it like this to create a new CollegeStudent object:您将像这样使用它来创建一个新的CollegeStudent object:

// this uses polymorphism as all CollegeStudnet are Student
Student studentX = new CollegeStundet("Tim", "Russel", "123AAA"); 

Third: Setting all the properties to the same value in the default constructor is really odd.第三:在默认构造函数中将所有属性设置为相同的值真的很奇怪。 In general, this will never be a good idea.一般来说,这绝不是一个好主意。 Provide at least on general constructor to initialize all three to different values.至少提供一个通用构造函数来将所有三个初始化为不同的值。 You can still let them be "empty" (null - when using a default constructor) when you already followed my first suggestion.当您已经遵循我的第一个建议时,您仍然可以让它们为“空”(null - 当使用默认构造函数时)。

 Student(string firstName, string lastName, string studentId);

 // the base keyword will pass the values to the constructor of Student
 CollegeStudent(string firstName, string lastName, string studentId) : base(firstName, lastName, studentId) 

But in general, I'd suggest to initialize them in the class where they are defined ( Student constructor in this case).但总的来说,我建议在定义它们的 class 中初始化它们(在这种情况下为Student构造函数)。 When you need to change the values from outside Student and you really want the values to be not writeable, there went something wrong with your concept.当您需要从Student外部更改值并且您真的希望这些值不可写时,您的概念出现了问题。

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

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