我有一组正在使用的接口(interface)。他们是IPatient、IDoctor 和IPerson。 IPerson 包含每个人的共享属性(姓名、性别、地址等)。我希望 IDoctor 和 IPatient 实现 IPerson 但是 this can't be done in C# according to this question ..
有什么办法可以消除 IPatient 和 IDoctor 之间的这些重复属性?
最佳答案
你说的重复在哪里?如果你看一个例子:
interface IPerson
{
string Name { get; set; }
}
interface IDoctor : IPerson
{
string Specialty {get; set; }
}
class Doctor : IDoctor
{
public string Name { get; set; }
public string Specialty {get; set;}
}
这里没有重复 -
Doctor
只需执行 Name
属性一次,当然也必须实现 Specialty
属性(property)。接口(interface)只为您提供接口(interface),而不是属性的实现(这正是您在大多数情况下想要利用多态性的方式) - 如果您需要这些属性的默认实现,您可能应该使用实现的抽象基类那些属性。
关于c# - 如何消除接口(interface)之间的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9231727/