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

标签 c# interface

是否可以实现如下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)的其他两种方法。

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

关于c# - 在 C# 中实现 2 个相似的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466613/

相关文章:

c++ - 转类 "Interfaceable"

Java 扩展 Map.entry() 而不使用泛型

c# - 具有多个定义的接口(interface) C#

c# - 构造函数和 With{} 哪个先出现?

c# - 无法访问在后端项目中上传的图片

interface - Go接口(interface)/容器使用

java - 在java中创建一个动态类

c# - Azure功能:我无法选择消费计划

c# - WPF 中的动态工具提示

c# - 将同一应用程序的两个实例写入同一日志文件