[英]define interface method with different parameters in 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){}
}
我想定义接口方法的参数名称和数据类型
设定新参数
这通常可以通过使用class
或struct
作为单个参数而不是内置类型来解决。
介面
您知道当class
实现一个熟悉的interface
时对它会有什么期望。 我们知道,所有实现IEnumerable
接口的类都可以在foreach
循环中使用。 按照惯例,接口的名称为“ I”,后跟对功能的描述。 该名称通常以后缀“ -able”结尾。
-able后缀形成形容词的含义:
1-可以计算。
2-具有[舒适的]质量。
牛津英语词典
让我们重命名parentInterface
和MethodA()
来给出一个清晰的示例,说明它通常如何工作(并避免负面制裁):
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.
}
}
我们在这里真正想念的是什么
我不了解生物学,但概念仍然相同。 您有一组ITreatable
疾病对象,它们需要具有GetTreatment()
方法。 但是,他们使用不同的标准进行计算。 我们需要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);
}
您有两种不同的方法
public String methodA(String a, int b, String c, long d){}
和
public String methodA(int e, String f, String g){}
分别代表childA和childB的两个不同合同。 您不能使用适合两个定义的单个methodA
定义接口。 您寻求做的是不可能的。
请注意,您可以在接口中定义两个重载,但是实现该接口的每个类都必须实现两个重载。
具有不同参数的方法不能都实现相同的接口方法声明。 如果您的方法签名与接口的签名不匹配,则说明您没有实现该接口。
您可以实现此目的,但是由于接口没有告诉您有关该方法的任何信息,因此它不是一个好的设计:
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;
}
}
您可以使用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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.