[英]How can I create a list with Interface Instances
I have an interface:我有一个界面:
public Interface IStudent
{
Students students {get;}
Boolean CanStayAfterHours;
}
public enum Students
{
Student1,
Student2,
Student3,
Student4
}
How can I add IStudent
properties to a generic list?如何将
IStudent
属性添加到通用列表中? List<IStudent>
? List<IStudent>
?
You first need to have a class that implements that interface:您首先需要有一个实现该接口的类:
public class Student : IStudent {
Students students { get; set; } // set, for example
Boolean CanStayAfterHours { get; set; }
}
Then you can add them to a list like this:然后你可以将它们添加到这样的列表中:
var studentList = new List<IStudent>() {
new Student() { CanStayAfterHours = true },
new Student() { CanStayAfterHours = false, Students = Students.Student1 },
new Student() { CanStayAfterHours = true },
};
Your design doesn't make much sense... but I'll leave that to you to figure out.你的设计没有多大意义……但我会把它留给你去弄清楚。
IList<IStudent>
can be used to keep a list of IStudent objects. IList<IStudent>
可用于保存 IStudent 对象的列表。 Of course you need a class Student
that implements IStudent
, because you cannot create an instance of an interface.当然,您需要一个实现
IStudent
class Student
,因为您无法创建接口的实例。 The purpose of your enum is unclear, you clearly don't want an enum value for each student, since that would require a rebuild of your application every time a new student signs up.您的枚举的目的尚不清楚,您显然不希望每个学生都有一个枚举值,因为每次新学生注册时都需要重新构建您的应用程序。
You'll need to implement a concrete object that implements the interface您需要实现一个实现接口的具体对象
public class ConcreteStudent : IStudent {公共类 ConcreteStudent : IStudent {
public Students students { get; set; }
public bool CanStayAfterHours { get; set; }
} }
Some changes where required to the interface, I added some setters to the interface, hope this agrees with your design需要对界面进行一些更改,我在界面中添加了一些设置器,希望这与您的设计一致
public interface IStudent {公共接口 IStudent {
Students students { get; set; }
Boolean CanStayAfterHours { get; set; }
} }
Then adding these objects to a list, we do the following然后将这些对象添加到列表中,我们执行以下操作
List<IStudent> students = new List<IStudent>()
students.Add(new ConcreteStudent()
{
students = Students.Student1,
});
interface ListofPatientService { List interface ListofPatientService { 列表
retriev();
}
class listofServiceDummy : ListofPatientService
{
List<patientmodel> list = new List<patientmodel>
{
new patientmodel(){Name="Abdi", Age= 30, Description=""},
new patientmodel(){Name="HassaN", Age= 40, Description=""},
new patientmodel(){Name="Hasna", Age= 35, Description=""},
new patientmodel(){Name="Moktar", Age= 50, Description=""},
new patientmodel(){Name="Liban", Age= 55, Description=""},
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.