简体   繁体   English

在C#中定义具有不同参数的接口方法

[英]define interface method with different parameters in C#

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

and

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 我想定义接口方法的参数名称和数据类型

Make a New Parameter 设定新参数

This can often be solved by using a class or struct to use as single parameter rather than the built-in Types. 这通常可以通过使用classstruct作为单个参数而不是内置类型来解决。

The Interface 介面

You know what to expect from a class when it implements a familiar interface . 您知道当class实现一个熟悉的interface时对它会有什么期望。 We know that all classes implementing the IEnumerable interface can be used in a foreach loop. 我们知道,所有实现IEnumerable接口的类都可以在foreach循环中使用。 By convention, the name of the interface is "I" followed by a description of an ability. 按照惯例,接口的名称为“ I”,后跟对功能的描述。 It is typical for the name to end with the suffix "-able". 该名称通常以后缀“ -able”结尾。

-able Suffix forming adjectives meaning: -able后缀形成形容词的含义:
1 -able to be [as in] calculable. 1-可以计算。
2 -having the quality of [as in] comfortable. 2-具有[舒适的]质量。

Oxford English Dictionary 牛津英语词典

Let's rename parentInterface and MethodA() to give a clear example of how this normally works (and to avoid negative sanctions): 让我们重命名parentInterfaceMethodA()来给出一个清晰的示例,说明它通常如何工作(并避免负面制裁):

public interface ITreatable
{
    Treatment GetTreatment();
}

Well, finding the cure may not be so easy, even if the object represents a treatable illness. 好吧,即使object代表可以治疗的疾病,找到治愈方法也并非那么容易。 Here's some examples: 以下是一些示例:

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.
    }
}

What We're Really Missing Here 我们在这里真正想念的是什么

I don't know biology, but the concept is still the same. 我不了解生物学,但概念仍然相同。 You have a group of ITreatable illness objects that need to have a GetTreatment() method; 您有一组ITreatable疾病对象,它们需要具有GetTreatment()方法。 however, they use different criteria for making calculations. 但是,他们使用不同的标准进行计算。 We need Symptoms . 我们需要Symptoms

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

Now, the objects can parse the symptoms in their own method, and our interface will look like this: 现在,对象可以使用自己的方法解析症状,我们的界面将如下所示:

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

You have two different methods 您有两种不同的方法

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

and

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

that represent two different contracts to childA and childB respectively. 分别代表childA和childB的两个不同合同。 You cannot define an interface with a single methodA that fits both definitions. 您不能使用适合两个定义的单个methodA定义接口。 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. 请注意,您可以在接口中定义两个重载,但是实现该接口的每个类都必须实现两个重载。

Methods with different parameters cannot both implement the same interface method declaration. 具有不同参数的方法不能都实现相同的接口方法声明。 If your method signature does not match that of the interface, you are not implementing the interface. 如果您的方法签名与接口的签名不匹配,则说明您没有实现该接口。

You can achieve this though, but it is not a good design since the interface is not telling you anything about the method: 您可以实现此目的,但是由于接口没有告诉您有关该方法的任何信息,因此它不是一个好的设计:

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;
    }
}

You could use an interface method with a variable number of arguments using the params keyword. 您可以使用params关键字将接口方法与可变数量的参数一起使用。 But you then need to cast each argument to the appropriate type, which is a bit error prone. 但是您随后需要将每个参数都强制转换为适当的类型,这容易出错。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM