如何在下面的代码中实现接口?
我想实现一个可在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个带有泛型类型参数的接口:
然后您可以声明您的类实现该接口,其中泛型参数被特定类型替换:
不过您仍然需要实现这些方法,就像您所做的那样。
如果您确实想避免代码冗余,则应该创建一个带有泛型类型参数的类(可能使用
abstract
关键字)。然后,您应该让原始类扩展该基类并从中删除公共代码。
如果这不仅仅是一个示例,您应该检查接口/抽象类定义的约定:
List
还是一个IReadOnlyList;
、ICollection
、IEnumerable
或Immutable*
之一类型足够;优选ViewAll()
返回的对象是否应该根据之后应用于ItemCollection
的修改而更改,还是应该保持不变?You can create an interface with a generic type parameter:
Then you can declare that your classes implement that interface where the generic parameter is replaced by a specific type:
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.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:
List<T>
or is anIReadOnlyList<T>
,ICollection<T>
,IEnumerable<T>
or one of theImmutable*
types enough ; preferableViewAll()
change according to modifications applied to theItemCollection
afterwards or should it stay the same?接口可以是通用的:
Interfaces can be generic:
定义接口
现在实现它
你已经有了正确的方法
话虽如此,我认为这对你没有多大帮助。通用接口可以工作,但不能提供您所追求的“即插即用”。
define the interface
now implement it
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.