在C#中使用不同的参数定义接口方法 [英] define interface method with different parameters in C#

查看:507
本文介绍了在C#中使用不同的参数定义接口方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface parentInterface
{
   public String methodA(/*define parameters name and dataType*/);
}

public class childA : parentInterface
{
  public String methodA(String a, int b, String c, long d){}
}

public class childB : parentInterface
{
   public String methodA(int e, String f, String g){}
}

我想定义接口方法的参数名称和数据类型

I want to define interface method's parameters name and data type

推荐答案

您有两种不同的方法

public String methodA(String a, int b, String c, long d){}

public String methodA(int e, String f, String g){}

分别代表childA和childB的两个不同合同.您不能使用适合两个定义的单个methodA定义接口.您试图做的事是不可能的.

that represent two different contracts to childA and childB respectively. You cannot define an interface with a single methodA that fits both definitions. What you seek to do is not possible.

请注意,您可以在接口中定义两个重载,但是实现该接口的每个类都必须实现两个重载.

Note that you could define both overloads in your interface, but then each class implementing that interface would have to implement both overloads.

这篇关于在C#中使用不同的参数定义接口方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆