简体   繁体   中英

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.

The Interface

You know what to expect from a class when it implements a familiar interface . We know that all classes implementing the IEnumerable interface can be used in a foreach loop. By convention, the name of the interface is "I" followed by a description of an ability. It is typical for the name to end with the suffix "-able".

-able Suffix forming adjectives meaning:
1 -able to be [as in] calculable.
2 -having the quality of [as in] comfortable.

Oxford English Dictionary

Let's rename parentInterface and MethodA() to give a clear example of how this normally works (and to avoid negative sanctions):

public interface ITreatable
{
    Treatment GetTreatment();
}

Well, finding the cure may not be so easy, even if the object represents a treatable illness. 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; however, they use different criteria for making calculations. We need 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. You cannot define an interface with a single methodA that fits both definitions. 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. 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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