需要一堂课的帮助?可能很简单

时间:2010-08-01 16:40:26

标签: c# c#-3.0 c#-4.0 c#-2.0

我想要做的是让UserID根据类的类型生成随机值,它应该是从person.cs生成的userID。 例如,学生班级生成一个学生ID和一个随机数,员工班应该生成一个员工ID和一个随机数,我遇到的问题是在控制台输出中显示任何用户ID样本数据,I没有任何数据显示用户ID任何人都可以帮助我。

以下是查看代码所需的链接

Person.cs

PersonTest.cs

Student.cs

Staff.cs

Faculty.cs

Person.cs

public class Person
{
    static string title;
    protected string firstName;
    protected string lastName;
    protected string address;
    protected string gender;
    protected string dateOfBirth;
    protected string userID;
    protected Random rnd = new Random();
    // constructors
    public Person()
    {

    }//end default constructor

    public Person(string aTitle, string aFirstName, string aLastName, string aAddress,
        string aGender, string aDateOfBirth)
    {
        title = aTitle;
        firstName = aFirstName;
        lastName = aLastName;
        address = aAddress;
        gender = aGender;
        dateOfBirth = aDateOfBirth;
        Console.WriteLine( this.DisplayInfo() );

        //create userID


        Console.WriteLine(userID);

        this.DisplayInfo();
    }//end 6-parameter constructor

刚添加

public string DisplayInfo()
    {
        return " You have created the person " + firstName + lastName +"\n whose address is" + address + "\n" + " whos is a " + gender + " and was born on " + dateOfBirth;
    }//end method DisplayInfo

Staff.cs student.cs现在有相同的数据

  public class Staff : Person
    {
        public Staff(string aTitle, string aFirstName, string aLastName, string aAddress,
           string aGender, string aDateOfBirth)
            : base(aTitle, aFirstName, aLastName, aAddress,
                    aGender, aDateOfBirth)
        {

           this.userID = firstName.Substring(0, 1) + lastName.Substring(0, 5);

           this.userID = this.userID + this.rnd.Next(1000, 9999);


        }

PersonTest.cs

  public class PersonTest
    {
       static void Main(string[] args) 
      { 
            Person testPerson = new Student("Mr.", "Merry ", "Lanes", " 493 Bluebane RD", "Male", " 8-06-1953 ");


            Person studentPerson = new Person("Mr.", "Jerry ", "Panes", " 493 Bluebane RD", "Male", " 8-06-1953 "); 

            // THIS DATA SHOWS UP BUT NOT THE USERID



            Console.ReadLine();
        }//end main

UML Diagram http://img529.imageshack.us/img529/5936/capturemv.jpg

3 个答案:

答案 0 :(得分:2)

您的UserID为空。

基类尝试在Staff类可以为UserID

赋值之前打印用户ID

您的问题位于您编写“创建用户ID”

的人员类中

如果你不熟悉这个过程的继承方式,那么你错过了关键步骤:

1:如果你从它的子类中调用父类构造函数,那么所有父类都会在子类之前运行。

正如您所看到的,userID将为null,如果您将"create userID"替换为userID = "1000";,则应该显示某些内容。

从我的观点来看,你只需要改变你的算法,希望这是一个简单的改变。

答案 1 :(得分:1)

好的,这结果是关于构造函数序列的问题。

始终首先调用基类构造函数。您可以以某种方式看到在派生类的语法中,对基本ctor的调用发生在参数列表和派生体之间。

我们有:

public class Person(....)
{
    protected string userID;

    public Person(....)
    {
      //create userID   //no, print it
      Console.WriteLine(userID);
    }
}

public class Staff : Person
{
    public Staff(...)
      : base(....)         // now it is printed
    {
       this.userID = ...;  // and now it is filled
    } 
}

顺便说一句,使用ID的随机数是有问题的。使用(静态ounter)生成0,1,2,3并且您没有碰撞的可能性。无论前缀如何。

答案 2 :(得分:0)

在我看来,你的问题是首先调用Person类的构造函数,然后调用Student(派生类)中的构造函数。请参阅另一篇关于base keyword的文章。

所以当你调用“Console.WriteLine(userID);”时在Person构造函数中,尚未分配用户ID。

尝试添加“Console.WriteLine(userID);”到Student类的构造函数。这适用于“新学生”电话。

根据您所显示的内容,Person类中没有userID的赋值(除非在“// create userID”之后有一些你没有向我们展示的内容),因此调用“new Person”将不会返回任何userID,因为它是一个空字符串。

顺便说一下。 Person构造函数中的最后一个语句“this.DisplayInfo();”没有理由,因为你没有在任何地方写它(你必须在DisplayInfo中使用Console.WriteLine,或使用“Console.WriteLine(this.DisplayInfo());”,如上面一行。

发布包含完整代码的链接,以便我们可以试用。