interface parentInterface
{
public String methodA(/*define parameters name and dataType*/);
}
Run Code Online (Sandbox Code Playgroud)
和
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){}
}
Run Code Online (Sandbox Code Playgroud)
我想定义接口方法的参数名称和数据类型
Tyl*_*uso 15
制作一个新参数
这通常可以通过使用class或struct作为单个参数而不是内置类型来解决.
界面
你知道什么class时候它会实现一个熟悉的interface.我们知道实现IEnumerable接口的所有类都可以在foreach循环中使用.按照惯例,界面名称为"I",后跟能力描述.典型的名称以后缀"-able"结尾.
-able 后缀形成形容词含义:
1 - 可以[如]计算.
2 - 具有[舒适]的质量.
牛津英语词典
让我们重新命名,parentInterface并MethodA()给出一个明确的例子,说明这通常是如何运作的(并避免负面制裁):
public interface ITreatable
{
Treatment GetTreatment();
}
Run Code Online (Sandbox Code Playgroud)
那么,找到治疗方法可能并不那么容易,即使它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.
}
}
Run Code Online (Sandbox Code Playgroud)
我们真的在这里缺少什么
我不知道生物学,但概念仍然是一样的.你有一组ITreatable需要有GetTreatment()方法的疾病对象; 但是,他们使用不同的标准进行计算.我们需要Symptoms.
public class Symptoms
{
public FamilyHistory History;
public DateTime Time;
public LabResult Lab;
public BloodTest BloodTest;
public TimeSpan SymptomTime;
public IsCritical IsCritical;
}
Run Code Online (Sandbox Code Playgroud)
现在,对象可以在自己的方法中解析症状,我们的界面将如下所示:
public interface ITreatable
{
Treatment GetTreatment(Symptoms symptoms);
}
Run Code Online (Sandbox Code Playgroud)
你有两种不同的方法
public String methodA(String a, int b, String c, long d){}
Run Code Online (Sandbox Code Playgroud)
和
public String methodA(int e, String f, String g){}
Run Code Online (Sandbox Code Playgroud)
分别代表 childA 和 childB 的两个不同合约。methodA您无法使用适合这两种定义的单一接口来定义接口。你想做的事情是不可能的。
请注意,您可以在接口中定义两个重载,但实现该接口的每个类都必须实现这两个重载。
| 归档时间: |
|
| 查看次数: |
11726 次 |
| 最近记录: |