我有3个子类“Staff,Faculty,Student”每个类都从父类人员获取用户的第一个姓氏,这个id喜欢在控制台应用程序运行时添加4个随机数。我遇到的问题是,所有的类都获得相同的4位数字我该如何解决这个问题,是的,它必须使用随机我不能使用静态计数器。消除随机重复?
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();
Faculty.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);
}
}
}
Staff.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
您的Person实现在哪里?定义了哪个rnd? – Blorgbeard 2010-08-02 14:20:16