在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){} } 

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

你有两种不同的方法

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

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

它分别代表与childA和childB的两个不同的合同。 您无法使用适合这两种定义的单个methodA定义接口。 你想做什么是不可能的。

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

创建一个新参数

这通常可以通过使用classstruct作为单个参数而不是内置类型来解决。

界面

您知道在实现熟悉的interface时对class期望。 我们知道实现IEnumerable接口的所有类都可以在foreach循环中使用。 按照惯例,界面的名称是“I”,后面跟着一个能力的描述。 典型的名称以后缀“-able”结尾。

-able后缀形成形容词含义:
1 – 可以计算。
2 – 具有[舒适]的质量。

牛津英语词典

让我们重命名parentInterfaceMethodA() ,给出一个明确的例子,说明这通常是如何工作的(并避免负面制裁):

 public interface ITreatable { Treatment GetTreatment(); } 

那么,找到治疗方法可能并不那么容易,即使该object代表了可治疗的疾病。 这是一些例子:

 public class TheFlu : ITreatable { public Treatment GetTreatment(int year) { // return some object, Treatment, based on the flu season. } } public class Hangover : ITreatable { public Treatment GetTreatment() { return Treatment.Empty; // no parameters necessary. } } public class Insomnia : ITreatable { public Treatment GetTreatment(FamilyHistory occurances, LabResult lab) { // return Some Treatment object that can be different based on the // calculated risk from the arguments. } } 

我们真的在这里失踪了什么

我不知道生物学,但概念仍然是一样的。 您有一组需要使用GetTreatment()方法的ITreatable疾病对象; 但是,他们使用不同的标准进行计算。 我们需要Symptoms

 public class Symptoms { public FamilyHistory History; public DateTime Time; public LabResult Lab; public BloodTest BloodTest; public TimeSpan SymptomTime; public IsCritical IsCritical; } 

现在,对象可以用自己的方法解析症状,我们的界面将如下所示:

 public interface ITreatable { Treatment GetTreatment(Symptoms symptoms); } 

您可以使用params关键字将接口方法与可变数量的参数一起使用。 但是,您需要将每个参数转换为适当的类型,这有点容易出错。

 public interface IFoo { void DoWork(params object [] arguments); } public class Foo : IFoo { public void DoWork(params object [] arguments) { string a = (string)arguments[0]; int b = (int)arguments[1]; string c = (string)arguments[2]; long d = (long)arguments[3]; Console.WriteLine("a={0}, b={1}, c={2}, d={3}", a,b,c,d); } } public class AnotherFoo : IFoo { public void DoWork(params object [] arguments) { int e = (int)arguments[0]; string f = (string)arguments[1]; string g = (string)arguments[2]; Console.WriteLine("e={0}, f={1}, g={2}", e,f,g); } } void Main() { var foo = new Foo(); foo.DoWork("a",1, "c",2L); var foo1 = new AnotherFoo(); foo1.DoWork(1,"f", "g"); } 

具有不同参数的方法不能同时实现相同的接口方法声明。 如果您的方法签名与接口的签名不匹配,则表示您没有实现该接口。

你可以实现这一点,但它不是一个好的设计,因为界面没有告诉你有关该方法的任何信息:

 interface parentInterface { string methodA(params object[] asd); } public class childA : parentInterface { public string methodA(params object[] p) { string a = p[0] as string; int b = (int)p[1]; string c = p[2] as string; long d = (long)p[3]; return string.Empty; } } public class childB : parentInterface { public string methodA(params object[] p) { int e = (int)p[0]; string f = p[1] as string; string g = p[2] as string; return string.Empty; } }