如何在下面的代码中实现接口?

发布于 2025-01-09 07:54:41 字数 889 浏览 1 评论 0原文

我想实现一个可在 Projct 和 Employe 类中使用的接口。我应该如何在接口中创建Add()和ViewAll()方法,这样我就不必重载类中的方法,同时声明方法名称相同,但参数不同。

public class Employe
    {
        public List<Employee> Employees { get; set; } = new List<Employee>();

        public void Add(Employee employee)
        {
            Employees.Add(employee);
        }

        public List<Employee> ViewAll()
        {
            return Employees;
        }

    }
    public class Projct
    {      

        public List<Project> Projects { get; set; } = new List<Project>();

        public void Add(Project project)
        {
            Projects.Add(project);
        }

        public List<Project> ViewAll()
        {
            return Projects;
        }
}

我理解接口就像一个合约,改变参数是没有意义的。但问题在于实施。另外,我已经看到了与此主题相关的大多数线程,看到了与声明参数类或使用参数相关的答案,甚至尝试这样做。我仍然无法弄清楚,所以如果有人可以从简单的角度解释,那将是受欢迎的。

I want to implement an interface which can be used in both Projct and Employe classes. How should I create Add() and ViewAll() methods in the interface, so that I don't have to overload the methods in classes while declaring as the method name is same, but the parameters are different.

public class Employe
    {
        public List<Employee> Employees { get; set; } = new List<Employee>();

        public void Add(Employee employee)
        {
            Employees.Add(employee);
        }

        public List<Employee> ViewAll()
        {
            return Employees;
        }

    }
    public class Projct
    {      

        public List<Project> Projects { get; set; } = new List<Project>();

        public void Add(Project project)
        {
            Projects.Add(project);
        }

        public List<Project> ViewAll()
        {
            return Projects;
        }
}

I understand that Interface is like a contract and it doesn't make sense to change the parameters. But the question is regarding implementation. Also, I have seen majority of threads related to this topic, saw answers related to declaring parameter class or using params and even tried doing that. I still can't figure it out, so if someone can explain from a simple perspective, that would be welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜宝宝 2025-01-16 07:54:41

您可以创建一个带有泛型类型参数的接口:

interface ItemCollection<T>
{
    void Add(T item);
    List<T> ViewAll();
}

然后您可以声明您的类实现该接口,其中泛型参数被特定类型替换:

public class Employe : ItemCollection<Employee> { ... }

public class Projct : ItemCollection<Project> { ... }

不过您仍然需要实现这些方法,就像您所做的那样。

如果您确实想避免代码冗余,则应该创建一个带有泛型类型参数的类(可能使用abstract 关键字)。

public class ItemCollection<T>
{
    private List<T> items = new List<T>();

    public void Add(T item)
    {
        items.Add(item);
    }
    
    public List<T> ViewAll()
    {
        return items;
    }
}

然后,您应该让原始类扩展该基类并从中删除公共代码。

如果这不仅仅是一个示例,您应该检查接口/抽象类定义的约定:

  • 您是否真的需要返回一个(可修改的)List 还是一个 IReadOnlyList;ICollectionIEnumerableImmutable* 之一类型足够;优选
  • ViewAll() 返回的对象是否应该根据之后应用于 ItemCollection 的修改而更改,还是应该保持不变?
  • 与现有的可用集合类型相比,您的类和接口有什么特别之处,这使得它们变得必要?

You can create an interface with a generic type parameter:

interface ItemCollection<T>
{
    void Add(T item);
    List<T> ViewAll();
}

Then you can declare that your classes implement that interface where the generic parameter is replaced by a specific type:

public class Employe : ItemCollection<Employee> { ... }

public class Projct : ItemCollection<Project> { ... }

You still have to implement the methods though, just as you did.

If you actually want to avoid the code redundancy, you should create a class (perhaps using the abstract keyword) with a generic type parameter instead.

public class ItemCollection<T>
{
    private List<T> items = new List<T>();

    public void Add(T item)
    {
        items.Add(item);
    }
    
    public List<T> ViewAll()
    {
        return items;
    }
}

You should then let your original classes extend that base class and remove the common code from them.

If this is not just an example you should review the contract defined by the interface / abstract class:

  • do you really need to return a (modifiable) List<T> or is an IReadOnlyList<T>, ICollection<T>, IEnumerable<T> or one of the Immutable* types enough ; preferable
  • Should the object returned by ViewAll() change according to modifications applied to the ItemCollection afterwards or should it stay the same?
  • What's special about your classes and the interface, compared to the existing collection types available which makes them necessary?
滥情空心 2025-01-16 07:54:41

接口可以是通用的:

public interface IMyInterface<T> {
    void Add(T t);
    List<T> ViewAll();
}

Interfaces can be generic:

public interface IMyInterface<T> {
    void Add(T t);
    List<T> ViewAll();
}
长途伴 2025-01-16 07:54:41

定义接口

public interface IAddAndDisplay{
     void Add(T employee);
      List<T> ViewAll();
}

现在实现它

public class Employee : IAddAndDisplay<Employee>
{

}

public class Project : IAddAndDisplay<Project>
{

}

你已经有了正确的方法

话虽如此,我认为这对你没有多大帮助。通用接口可以工作,但不能提供您所追求的“即插即用”。

define the interface

public interface IAddAndDisplay{
     void Add(T employee);
      List<T> ViewAll();
}

now implement it

public class Employee : IAddAndDisplay<Employee>
{

}

public class Project : IAddAndDisplay<Project>
{

}

You already have the correct methods

Having said all that I dont think this will help you very much. Generic interfaces work but dont provide the 'plug and play ' that you are after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文