我有一个非常简单的场景:一个“人”可以是公司的“客户”或“员工”。
使用“Call”方法可以通过电话称呼一个“人”。
根据“人”在通话上下文中扮演的角色,例如新产品的宣布或组织变更的公告,我们应该使用为“客户”角色提供的电话号码或为“员工”角色提供的电话号码。
以下是情况总结:
但是这段代码无法编译,会产生以下错误:
这种情况在C#中是否有其他实现方式,还是我需要寻找其他设计方案?
如果有的话,你有什么替代方案建议?
感谢您的帮助。
使用“Call”方法可以通过电话称呼一个“人”。
根据“人”在通话上下文中扮演的角色,例如新产品的宣布或组织变更的公告,我们应该使用为“客户”角色提供的电话号码或为“员工”角色提供的电话号码。
以下是情况总结:
interface IPerson
{
void Call();
}
interface ICustomer : IPerson
{
}
interface IEmployee : IPerson
{
}
class Both : ICustomer, IEmployee
{
void ICustomer.Call()
{
// Call to external phone number
}
void IEmployee.Call()
{
// Call to internal phone number
}
}
但是这段代码无法编译,会产生以下错误:
error CS0539: 'ICustomer.Call' in explicit interface declaration is not a member of interface
error CS0539: 'IEmployee.Call' in explicit interface declaration is not a member of interface
error CS0535: 'Both' does not implement interface member 'IPerson.Call()'
这种情况在C#中是否有其他实现方式,还是我需要寻找其他设计方案?
如果有的话,你有什么替代方案建议?
感谢您的帮助。
((IPerson)new Both()).Call()
,你希望发生什么? - SLaks