Skip to main content
 首页 » 编程设计

c#之在 C# 中实现 2 个相似的接口(interface)

2024年02月13日21itcoder

是否可以实现如下2个接口(interface)?这段代码可以编译,但在阅读了如何实现这些代码后,我不确定这是否可行。

Interface1 
{ 
   Authorize(string p1, int p2); 
   LookupCode(int test); 
   GetObject(int ID); 
} 
 
Interface2 
{ 
   Authorize(string p1); 
   LookupCode(int test); 
   GetObject(int ID); 
} 
 
class WebService : Interface1, Interface2 
{ 
   Authorize(string p1, int p2) 
   { 
   } 
 
   Authorize(string p1) 
   { 
   } 
 
   LookupCode(int test) 
   { 
   } 
   GetObject(int ID) 
   { 
   } 
} 

因为 Authorize 方法具有不同的签名,它们是否可以正确映射到正确的接口(interface)?

其他相同的方法具有相同的实现,并且在 WebService 类中只有 1 个实例。这样也可以吗?

请您参考如下方法:

Because the Authorize methods have different signatures, could they be correctly mapped to the correct interface?

当然。事实上,这就是为什么可以将它们分别正确映射到自己的接口(interface),而不是同时映射到两个接口(interface)的其他两种方法。

注意:您的代码有明显的语法错误。但是,由于您提到您的代码可以编译,我假设您的实际代码已修复这些错误。