我有 3 个子类“员工、教师、学生”,每个类都从父类人那里获取用户的第一个初始姓氏,在运行控制台应用程序时,向这个 id 添加 4 个随机数。我遇到的问题是所有类都获得相同的 4 位数字,我该如何解决这个问题,是的,它必须使用 Random 来完成,我不能使用静态计数器。
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();
学院.cs
namespace Person
{
public class Faculty : Person
{
public Faculty(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 + rnd.Next(1000, 9999);
Console.WriteLine(this.userID);
}
}
}
员工.cs
namespace Person
{
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 + rnd.Next(1000, 9999);
Console.WriteLine(userID);
}
}
}
测试班
public class PersonTest
{
static void Main(string[] args)
{
Person testPerson = new Faculty(" Mr.", "Merry ", "Lanes", " 493 Bluebane RD", "Male", " 8-06-1953\n ");
Person studentPerson = new Student(" Mr.", "Jerry ", "Panes", " 456 Greenbane RD", "Male", " 8-10-1945\n");
Person staffPerson = new Staff(" Mr.", "Joe ", "Gaines", " 495 Redbane RD", "Male", " 8-21-1989\n ");
Console.ReadLine();
}//end main