C# 在C中定义具有不同参数的接口方法#
及C# 在C中定义具有不同参数的接口方法#,c#,interface,C#,Interface,及 我想定义接口方法的参数名称和数据类型具有不同参数的方法不能同时实现相同的接口方法声明。如果您的方法签名与接口的签名不匹配,则说明您没有实现接口 虽然您可以实现这一点,但这不是一个好的设计,因为界面没有告诉您有关该方法的任何信息: public class childA : parentInterface { public String methodA(String a, int b, String c, long d){} } public class childB : parentI
我想定义接口方法的参数名称和数据类型具有不同参数的方法不能同时实现相同的接口方法声明。如果您的方法签名与接口的签名不匹配,则说明您没有实现接口 虽然您可以实现这一点,但这不是一个好的设计,因为界面没有告诉您有关该方法的任何信息:
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){}
}
你有两种不同的方法
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;
}
}
及
分别代表childA和childB的两份不同合同。您不能使用一种既适合这两种定义的方法a
来定义接口。你想做的事是不可能的
请注意,您可以在接口中定义这两个重载,但是实现该接口的每个类都必须实现这两个重载。您可以使用带有可变数量参数的接口方法,使用
params
关键字。但随后需要将每个参数转换为适当的类型,这很容易出错
public String methodA(int e, String f, String g){}
创建新参数 这通常可以通过使用
类
或结构
作为单个参数而不是内置类型来解决
界面
当类实现熟悉的接口
时,您知道该从中得到什么。我们知道实现IEnumerable
接口的所有类都可以在foreach
循环中使用。按照惯例,接口的名称是“I”,后面是对某个能力的描述。名称通常以后缀“-able”结尾
-能干
构成后缀的形容词,意思:
1-能够[如在]可计算
具有舒适的品质
让我们重命名parentInterface
和MethodA()
,以给出一个明确的示例,说明这通常是如何工作的(并避免负面制裁):
嗯,找到治疗方法可能并不容易,即使对象
代表一种可治疗的疾病。以下是一些例子:
public interface ITreatable
{
Treatment GetTreatment();
}
我们在这里真正缺少的东西
我不懂生物学,但概念还是一样的。您有一组需要使用GetTreatment()
方法的ITreatable
disease对象;但是,他们使用不同的标准进行计算。我们需要症状
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.
}
}
现在,对象可以用自己的方法解析症状,我们的界面如下所示:
public class Symptoms
{
public FamilyHistory History;
public DateTime Time;
public LabResult Lab;
public BloodTest BloodTest;
public TimeSpan SymptomTime;
public IsCritical IsCritical;
}
这违背了接口的目的我很不清楚你在做什么,但是听起来这个接口应该是通用的…你想解决什么问题呢?你总是可以让childB
实现childA
从方法a调用它的版本,默认值为d
,但是不知道方法a实际做了什么,很难说这是不是一个好主意(我想不是)@chole:很多人都花时间给你提了一些建议。对建议发表评论将是一种尊重,并对任何有帮助的事情进行投票不仅是不可能的,而且从根本上说是不正确的。接口是定义一个合同。如果你不知道一个方法调用需要什么参数,这有什么用。@DavidPilkington:我的意思就是说两个不同的重载代表两个不同的契约。
public interface ITreatable
{
Treatment GetTreatment();
}
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.
}
}
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);
}