我有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