如何在C类中实现接口#

时间:2022-06-01 17:50:57

i have two interfaces IAppointment and IAppointments : IList<IAppointment> in the second class i have 3 members

我有两个接口IAppointment和IAppointments:IList 在第二类我有3个成员

 public interface IAppointments : IList<IAppointment>
{
    bool Load();
    bool Save();
    IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date);
}

Of which i can only implement the first 2 in the class Appointments and i get errors with whatever method i tried for the 3rd one and i get the same 14 errors always (about "Appointments does not implement interface member IAppointment.GetEnumerator() , .Count , .Remove , .Contains and some others

其中我只能实现Appointments类中的前2个,并且我用第3个方法尝试的任何方法都会出错,并且我总是得到相同的14个错误(关于“约会没有实现接口成员IAppointment.GetEnumerator(),. Count,.Remove,.Contains和其他一些

also this is the other one

这也是另一个

public interface IAppointment
{
    DateTime Start { get; }
    int Length { get; }
    string DisplayableDescription { get; }
    bool OccursOnDate(DateTime date);
}

where here i probably need to implement those in a class too , Sorry about my bad explanation but maybe i havent understood the actual problem

在这里我可能也需要在课堂上实现这些,抱歉我的错误解释但也许我还没有理解实际的问题

P.S. both the interface/classes are being used in another partial class which runs without errors

附:接口/类都在另一个无错运行的部分类中使用

Update:

更新:

My only problem now is that i don't know how to implement the 1st member of the IAppointment (What should it's return type be?? since its the Starting time of the Appointment e.g. 12:00) almost everything else is fine i think

我现在唯一的问题是我不知道如何实现IAppointment的第一个成员(它的返回类型应该是什么?因为它的任命的开始时间,例如12:00)几乎其他一切都很好我认为

P.S.2 Thank you guys for your help so far!

P.S.2到目前为止,谢谢你们的帮助!

2 个解决方案

#1


0  

As noted in the comments, you cannot only implement a specific set of methods for an interface and as your IAppointments interface derives from IList<IAppointment>, the implementing class must also implement all members of the IList interface in addition to the members of IAppointments.

如注释中所述,您不仅可以为接口实现一组特定的方法,而且您的IAppointments接口派生自IList ,除了IAppointments的成员之外,实现类还必须实现IList接口的所有成员。

The following class definition will achieve this:

以下类定义将实现此目的:

using System.Collections.ObjectModel;

public class Appointments : Collection<IAppointment>, IAppointments
{
    public bool Load()
    {
        return true;
    }

    public bool Save()
    {
        return true;
    }

    public IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date)
    {
        return new List<IAppointment>();
    }
}

This will give you access to all the methods on IList (since Collection<T> implements IList<T>) and IAppointment allowing you to write code such as the following (assuming your class that implements IAppointment is called Appointment and I have divined the intentions of your code correctly):

这将使您可以访问IList上的所有方法(因为Collection 实现IList )和IAppointment允许您编写如下代码(假设您实现IAppointment的类称为约会,我已经确定了意图你的代码正确):

var appointments = new Appointments();

if (appointments.Load() == true) // from IAppointments
{
    var totalAppointmentCount = appointments.Count(); // from IList through Collection<T>
    var numberOfAppointmentsToday = appointments.GetAppointmentsOnDate(DateTime.Now.Date).Count(); // from IAppointments

    var newAppointment = new Appointment();

    appointments.Add(newAppointment); // from IList through Collection<T>

    if (appointments.Save() == true) // from IAppointments
    {
        Console.WriteLine("All saved, happy days!");
    }
}

#2


1  

Because your IAppointments interface derives from IList<T>, your Appointments class must implement all members of IList<T> and any interfaces which that interface derives from. GetEnumerator() comes from IEnumerable<T>, which IList<T> derives from.

因为您的IAppointments接口派生自IList ,所以您的Appointments类必须实现IList 的所有成员以及该接口派生的所有接口。 GetEnumerator()来自IEnumerable ,IList 派生自。

Unless you use an approach such as composition where you expose an IList<T> property on IAppointments to get a list on which to perform operations such as indexing etc., you will need to implement all the members of IList<T>, ICollection<T> and IEnumerable<T> in your Appointments class.

除非您使用诸如组合之类的方法在IAppointments上公开IList 属性以获取执行索引等操作的列表,否则您将需要实现IList 的所有成员,ICollection < Appointments类中的T>和IEnumerable

I think your best solution is something like this:

我认为你最好的解决方案是这样的:

public interface IAppointments
{
    IList<IAppointment> TheAppointments { get; }

    bool Load();
    bool Save();
    IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date);
}

Then you would access the TheAppointments property in your Appointments class to provide the base of your implementation of GetAppointmentsOnDate(DateTime).

然后,您将访问Appointments类中的TheAppointments属性,以提供GetAppointmentsOnDate(DateTime)实现的基础。

#1


0  

As noted in the comments, you cannot only implement a specific set of methods for an interface and as your IAppointments interface derives from IList<IAppointment>, the implementing class must also implement all members of the IList interface in addition to the members of IAppointments.

如注释中所述,您不仅可以为接口实现一组特定的方法,而且您的IAppointments接口派生自IList ,除了IAppointments的成员之外,实现类还必须实现IList接口的所有成员。

The following class definition will achieve this:

以下类定义将实现此目的:

using System.Collections.ObjectModel;

public class Appointments : Collection<IAppointment>, IAppointments
{
    public bool Load()
    {
        return true;
    }

    public bool Save()
    {
        return true;
    }

    public IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date)
    {
        return new List<IAppointment>();
    }
}

This will give you access to all the methods on IList (since Collection<T> implements IList<T>) and IAppointment allowing you to write code such as the following (assuming your class that implements IAppointment is called Appointment and I have divined the intentions of your code correctly):

这将使您可以访问IList上的所有方法(因为Collection 实现IList )和IAppointment允许您编写如下代码(假设您实现IAppointment的类称为约会,我已经确定了意图你的代码正确):

var appointments = new Appointments();

if (appointments.Load() == true) // from IAppointments
{
    var totalAppointmentCount = appointments.Count(); // from IList through Collection<T>
    var numberOfAppointmentsToday = appointments.GetAppointmentsOnDate(DateTime.Now.Date).Count(); // from IAppointments

    var newAppointment = new Appointment();

    appointments.Add(newAppointment); // from IList through Collection<T>

    if (appointments.Save() == true) // from IAppointments
    {
        Console.WriteLine("All saved, happy days!");
    }
}

#2


1  

Because your IAppointments interface derives from IList<T>, your Appointments class must implement all members of IList<T> and any interfaces which that interface derives from. GetEnumerator() comes from IEnumerable<T>, which IList<T> derives from.

因为您的IAppointments接口派生自IList ,所以您的Appointments类必须实现IList 的所有成员以及该接口派生的所有接口。 GetEnumerator()来自IEnumerable ,IList 派生自。

Unless you use an approach such as composition where you expose an IList<T> property on IAppointments to get a list on which to perform operations such as indexing etc., you will need to implement all the members of IList<T>, ICollection<T> and IEnumerable<T> in your Appointments class.

除非您使用诸如组合之类的方法在IAppointments上公开IList 属性以获取执行索引等操作的列表,否则您将需要实现IList 的所有成员,ICollection < Appointments类中的T>和IEnumerable

I think your best solution is something like this:

我认为你最好的解决方案是这样的:

public interface IAppointments
{
    IList<IAppointment> TheAppointments { get; }

    bool Load();
    bool Save();
    IEnumerable<IAppointment> GetAppointmentsOnDate(DateTime date);
}

Then you would access the TheAppointments property in your Appointments class to provide the base of your implementation of GetAppointmentsOnDate(DateTime).

然后,您将访问Appointments类中的TheAppointments属性,以提供GetAppointmentsOnDate(DateTime)实现的基础。