我在C#中创建了一个列表员工数据应用程序,其功能包括AddEmployee,DeleteEmployee,SearchEmployee。我想制作相同的程序,但使用Singleton设计模式。我知道Singleton的结构,但我搞砸了列表,然后将实际的员工添加到列表中。
以下是我的计划的一部分:
class Student : IComparable<Student>
{
//public int id;
public string name;
public decimal salary;
public string position;
public int intern;
public Student(string name, string position, int intern, decimal salary)
{
this.name = name;
this.position = position;
this.intern = intern;
this.salary = salary;
}
public string Name { get; set; }
public decimal Salary { get; set; }
public string Position { get; set; }
public int Intern { get; set; }
public void CalculateSalary()
{
salary = 1000 * intern;
}
public override string ToString()
{
return "Name: " + name + "\t Position: " + position + "\t Intern: " + intern + "\t Salary: " + salary;
}
/// <summary>
/// Gets student information from the user and adds a new student to the list
/// </summary>
/// <param name="existingStudents">The list of students to add to</param>
private static void AddStudent(List<Student> existingStudents)
{
string ans = "";
do{
// Initialize the list if it's null
if (existingStudents == null) existingStudents = new List<Student>();
int tempInt;
// Get student information from the user
Console.WriteLine("Enter new student information");
Console.Write(" 2. Enter student Name: ");
var name = Console.ReadLine();
Console.Write(" 3. Enter student Job Title: ");
var jobTitle = Console.ReadLine();
do
{
Console.Write(" 4. Enter student years of service: ");
} while (!int.TryParse(Console.ReadLine(), out tempInt));
var yrsOfService = tempInt;
var salar = tempInt;
// Add the new student to the list
existingStudents.Add(new Student(name, jobTitle, yrsOfService, salar));
foreach (Student stud in existingStudents)
{
stud.CalculateSalary();
Console.WriteLine(stud.ToString());
}
Console.WriteLine("Do you want to add another Student? y/n");
ans = Console.ReadLine();
}
while (ans != "n");
}
在主程序中,我有一个列表并调用addStudent函数:
List<Student> st = new List<Student>();
int answer = 1;
do
{
Console.WriteLine("============================");
Console.WriteLine("2. Read From File");
Console.WriteLine("3. Add Student...");
answer = Convert.ToInt32(Console.ReadLine());
switch (answer)
{
case 2:
JustReadFromFile(st, filePath);
break;
case 3:
AddStudent(st);
break;
case 4:
Console.WriteLine("Exit Menu...");
break;
}
} while (answer != 4);
列出st = new List();
我知道我必须创建一个私有静态实例。
private static Student st;
此外,应该有一个私人学生构造函数,而不是上面的。
private Student() { }
这是创造新学生的方法。我也认为我需要一个类似的方法来创建我的列表,但我不确定。
public static Student getInstance()
{
if (st == null)
st = new Student();
return st;
}
请你给我一些指示和建议。谢谢
答案 0 :(得分:0)
@bottom-center
{
content: $space.getName();
}
然后您可以像这样访问Student实例: Student.Instance.SomeMethod();
答案 1 :(得分:0)
Student
上课:
public class Student
{
public string Name { get; set; }
public decimal Salary { get; set; }
public string Position { get; set; }
public int Intern { get; set; }
}
StudentData
singleton(您也可以将List<Student
私有并在Singleton类中创建方法,以便与列表进行交互并获取/添加数据,但我将其公开给了简单的例子):
public class StudentData
{
private static readonly StudentData _studentData = new StudentData();
public List<Student> Students { get; set; }
private StudentData()
{
Students = new List<Student>();
}
public static StudentData GetInstance()
{
return _studentData;
}
}
这样的用法:
var studentData = StudentData.GetInstance();
//Get all students in the singleton
var students = studentData.Students;
//Add new student
studentData.Students.Add(new Student());
studentData.Students.Add(new Student
{
Intern = 1,
Name = "SomeName"
});
希望这会让你走向正确的方向。