簡體   English   中英

在C#中定義具有不同參數的接口方法

[英]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){}
}

我想定義接口方法的參數名稱和數據類型

設定新參數

這通常可以通過使用classstruct作為單個參數而不是內置類型來解決。

介面

您知道當class實現一個熟悉的interface時對它會有什么期望。 我們知道,所有實現IEnumerable接口的類都可以在foreach循環中使用。 按照慣例,接口的名稱為“ I”,后跟對功能的描述。 該名稱通常以后綴“ -able”結尾。

-able后綴形成形容詞的含義:
1-可以計算。
2-具有[舒適的]質量。

牛津英語詞典

讓我們重命名parentInterfaceMethodA()來給出一個清晰的示例,說明它通常如何工作(並避免負面制裁):

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM