【发布时间】:2010-08-01 16:40:25
【问题描述】:
我想要做的是让 UserID 根据类的类型生成随机值,因为 userID 应该从 person.cs 生成。 例如,学生类生成一个学生 id 和一个随机数,员工类应该生成一个员工 id 和一个随机数,我遇到的问题是让任何用户 ID 样本数据完全显示在控制台输出中,我没有任何数据显示用户 ID 任何人都可以帮助我。
这是查看代码所需的链接
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
【问题讨论】:
-
“显示”数据的代码在哪里? (我假设您的意思是打印到控制台?)
-
您的意思是构建一个工作人员来代替学生吗?你显示 Staff.cs。
-
如果要显示数据,请将其打印到控制台。
-
你能告诉我们你在哪里显示数据的代码吗?
-
抱歉停电了,这就是我想弄清楚我该怎么做?