简体   繁体   English

如何使用接口实例创建列表

[英]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.

相关问题 如何创建从实现接口的基类派生的实例列表? - How to create list of instances deriving from a base class that implements interface? 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T 在 Blazor 中,如何使用 EventCallback 创建接口? - In Blazor, how can I create an interface with EventCallback? 如何创建通用委托实例列表? - How do I create a list of generic delegate instances? <I>给定A的实例(A源自B且B实现I),</i>如何创建<I>包含B实例的</i>列表<I>?</i> - How to create a List<I> containing instances of B, given instances of A (A derives from B and B implements I)? 我可以创建通用接口吗? - Can I create a generic interface 如何在VB.NET中使用隐式实现创建接口 - How can I create an interface in VB.NET with implicit implementations 如何创建和使用作为 class 实例的项目列表 - How to create and use a list of items that are instances of a class 单身 - 我可以创建多个实例 - Singleton - I can create multiple instances 如果类实例在数组中,我可以列出一个数字吗? - Can I list a number if class instances in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM