将对象的内容添加到列表而不是参数类型
本文关键字:参数 类型 列表 对象 添加 | 更新日期: 2023-09-27 18:09:00
希望我描述的足够正确和清楚。我试图保存由用户输入的多个详细信息,并将它们保存到列表。然而,目前我这样做的方式是只保存对象类型/名称,而不是数据。下面是我的代码,我如何保存对象的数据,而不是对象的名称?
Student stud = new Student();
stud.Enter_Student();
_studentList.Add(stud);
输入学生class Student : Person
{
public string StudentId { get; private set; }
public string Subject { get; private set; }
//string[] _studentdb = new string[4];
public Student()
{
StudentId = "abc123";
Subject = "Building Subject";
}
public void Enter_Student()
{
this.Person_Prompt_Print(); //Prompts for user
this.Address_Prompt_Print();
this.Contact_Prompt_Print();
Console.SetCursorPosition(4, 18);
Console.WriteLine("Student ID:");
this.Enter_Person(); // Inputs from user
this.Enter_Address();
this.Enter_Contacts();
StudentId = Console.ReadLine();
Console.SetCursorPosition(30, 18);
}
}
person类的日期样本
class Person
{
Tidyup tidy = new Tidyup();
public string FirstName { get; private set; }
public string Surname { get; private set; }
public string MiddleName { get; private set; }
public string AddressLine1 { get; private set; }
public string AddressLine2 { get; private set; }
public string Town { get; private set; }
public string Postcode { get; private set; }
public string Email { get; private set; }
public string Telephone { get; private set; }
public Person()
{
FirstName = "Name";
Surname = "Surname";
MiddleName = "Middle Name";
AddressLine1 = "Address";
AddressLine2 = "Address Ln2";
Town = "Town";
Postcode = "<xxx>/<xxx>";
Email = "name@buildright.ac.uk";
Telephone = "0800 0000000";
}
public void Person_Prompt_Print()
{
// Program Frame
tidy.Line_Top();
tidy.Line_Base();
tidy.Sides_Left();
tidy.Sides_Right();
Console.SetCursorPosition(4, 2); //Prompts for user
Console.WriteLine("FirstName:");
Console.SetCursorPosition(4, 4);
Console.WriteLine("Surname:");
Console.SetCursorPosition(4, 6);
Console.WriteLine("Middle Name:");
}
public void Address_Prompt_Print()
{
Console.SetCursorPosition(4, 8); //Prompts for user
Console.WriteLine("House Number/Name:");
Console.SetCursorPosition(4, 10);
Console.WriteLine("Street:");
Console.SetCursorPosition(4, 12);
Console.WriteLine("Town:");
Console.SetCursorPosition(4, 14);
Console.WriteLine("Post Code:");
}
public void Contact_Prompt_Print()
{
Console.SetCursorPosition(4, 16);
Console.WriteLine("Email:");
Console.SetCursorPosition(4, 18);
Console.WriteLine("Telephone:");
}
public void Enter_Person()
{
Console.SetCursorPosition(30, 2); // Inputs from user
FirstName = Console.ReadLine();
Console.SetCursorPosition(30, 4);
Surname = Console.ReadLine();
Console.SetCursorPosition(30, 6);
MiddleName = Console.ReadLine();
}
public void Enter_Address()
{
Console.SetCursorPosition(30, 8); // Inputs from user
AddressLine1 = Console.ReadLine();
Console.SetCursorPosition(30, 10);
AddressLine2 = Console.ReadLine();
Console.SetCursorPosition(30, 12);
Town = Console.ReadLine();
Console.SetCursorPosition(30, 14);
Postcode = Console.ReadLine();
}
public void Enter_Contacts()
{
Console.SetCursorPosition(30, 16);
Email = Console.ReadLine();
Console.SetCursorPosition(30, 18);
Telephone = Console.ReadLine();
}
} // End of Class
最后我通过一个简单的嵌套foreach循环打印出来
public void Print_all_student()
{
Console.Clear();
foreach (Student t in _studentList)
{
// print another list items.
foreach (Student t1 in _studentList)
{
Console.WriteLine("/" + t + "/" + t1);
}
}
Console.ReadKey();
}
如果有人能帮助我了解我错过了什么,以及如何访问数据打印出来,我将不胜感激。
这里有很多问题,但是在您的Console.WriteLine
调用中,您只显示Student
类型,因此将调用Student
类型上的ToString
方法,默认情况下将显示类型名称。
您想要显示Student
类型的单个属性,例如
foreach (Student student in studentList)
{
Console.WriteLine(student.FirstName);
Console.WriteLine(student.Surname);
// etc
}
请记住,Student
派生自Person
,所以所有的公共属性都可以从Student
引用访问,因为Student
是Person
。
:
- 你有一个冗余循环-你只需要一个循环枚举
studentList
- 这里确实有各种各样的担忧。你的
Student
和Person
类型不应该与UI有关(即与Console
调用有关的任何事情) - 坚持使用PascalCase (aka UpperCamelCase)作为你的方法名,没有下划线