簡體   English   中英

在接口中定義一個接受 n 個參數的方法

[英]Define a method in interface which takes n parameters

我正在嘗試定義一個接口和實現接口的類,如下所示。 接口中定義的方法接受一個字符串作為參數,其中方法Execute myClass2 實現采用 2 個不遵循接口定義的參數。

那就是問題所在。 我怎么能在一個接口中定義一個方法,它接受 n 個不同類型的參數?

請指教。 謝謝。

public interface MyInterface
{
     void Execute(string a);
}

public class myClass1 : MyInterface
{
     public void Execute(string a)
     {
        Console.WriteLine(a);
     }
}

public class myClass2 : MyInterface
{
     public void Execute(string a, int b)
     {
          Console.WriteLine(a);
          Console.WriteLine(b.ToString());
     }

}

編輯:我正在考慮另一種方法。 如果有人能告訴我這是否是一個更好的設計,我很感激。

public interface IParameter
{
    Type ParameterType { get; set; }
    string Name { get; set; }
    object Value { get; set; }
}

public interface MyInterface 
{
     void Execute(Recordset recordSet, List<IParameter> listParams);
}

public class MyClass : MyInterface
{
      public void Execute(Recordset recordSet, List<IParameter> listParams)
      {

      }
}

我正在傳遞一個 IParameter 列表,其中包含需要發送的所有必需參數。

如果接口沒有修復參數類型,調用者如何知道如何調用方法?

最接近的可能是:

public interface MyInterface
{
     void Execute(params object[] args);
}

接口的實現然后必須處理傳入的任意數量的參數 - 你不能有一個處理單個int參數的實現,盡管如果args包含單個args以外的任何內容,它當然會拋出異常int數值。

編輯:明確地說,這很少是一個好的設計。 在一些非常弱類型的場景中它可能是合適的,但除此之外,通常值得嘗試找到更好的東西。

如果您可以提供有關您正在嘗試做什么的更多信息,我們或許可以為您提供更多幫助。

你不能這樣做是有充分理由的。 接口的不同實現旨在互換使用。 您提出的設計違反了這一原則。 如果您需要幫助解決沖突,我認為您需要解釋是什么導致您采用這種設計。

所以你將你的界面定義為

public interface MyInterface
{
    void Execute(string a);
}

並試圖將其實現為

public void Execute(string a, int b)
{
    ...
}

那是行不通的——你聲明了一個接口,並試圖定義其他的東西。

什么可能有效(到目前為止我無法根據您的帖子判斷)是顯式接口實現 - 也就是說,您的具體對象可以公開 Execute(string, int) 方法並顯式實現您的接口方法。 就像是

public class myClass2 : MyInterface
{
    public void Execute(string a, int b)
    {
        ...
    }

    void MyInterface.Execute(string a)
    {
        ...
    }
}

也就是說,我強烈建議您重新考慮這種設計。 接口的全部意義在於,它們向您的其余代碼公開了一個通用的編程表面 - 就代碼氣味而言,破壞該合同的后果很嚴重。

就其價值而言,這可能是泛型的一個很好的用例。

您將所需的最少參數定義為接口的屬性,然后在需要更多參數的地方繼承。

當您在基本接口中只使用 1 個參數時看起來很傻,但當然這個概念可以擴展到更復雜的類型。

public interface MyInterface<T> where T : ParamA
{
  void Execute(T paramA);
}

public interface ParamA
{
  string ParameterA { get; }
}

public class myClass1 : MyInterface<myClass1.myParamA>
{
  public class myParamA : ParamA
  {
    public string ParameterA { get; set; }
  }

  public void Execute(myParamA a)
  {
    Console.WriteLine(a.ParameterA);
  }
}

public class myClass2 : MyInterface<myClass2.myParamsAb>
{
  public class myParamsAb : ParamA
  {
    public string ParameterA { get; set; }
    public int ParameterB { get; set; }
  }

  public void Execute(myParamsAb ab)
  {
    Console.WriteLine(ab.ParameterA);
    Console.WriteLine(ab.ParameterB.ToString());
  }
}

您可以使用弱類型方法來做到這一點。 例如,您可以定義一個接受對象數組的接口:

public intrface MyInterface
{
  void Execute(params object[] args);
}

而且您可以使用任何參數調用任何具體類:

myClass.Execute("string", 1);

但在這種情況下,您違反了接口、繼承和編譯時檢查的主要目的。

實現這一點的另一種方法是將所有參數封裝在額外的類層次結構中:

class CommandData
{
  public string StringData {get; set;}
}

class ExtendedCommandData : CommandData
{
  public int I {get;set;}
}

interface IMyInterface
{
  public void Execute(CommandData commandData);
}

class MyClass1 : IMyInterface
{
  public void Execute(CommandData commandData);
}

class MyClass2 : IMyInterface
{
  // Lets impelment this interface explicitely
  void IMyInterface.Execute(CommandData commandData)
  {
  }

  void Execute(ExtendedCommandData extendedData)
  {
    // now we can access to string and int parameter
  }
}

除了@Jon 回答:考慮到您正在實現一個接口,所以您是架構師,只是不要使用接口,而是使用具有重載虛函數的簡單基類,並在每個具體類中以您喜歡的方式對其進行 ocerride。

編輯:我的意思是這樣的:不是使用接口聲明基類,而是偽代碼!

public class MyCoolBase // a base CLASS and not interface
{
     public virtual void Execute(string a)
     {
        //empty, or NotImplementedException, base on design decision
     }

     public virtual void Execute(double b)
     {
        //empty, or NotImplementedException, base on design decision
     }
     public virtual void Execute(int a, int b)
     {
        //empty, or NotImplementedException, base on design decision
     }
}

public class MyCoolChildOne : MyCoolBase  
{
     public override void Execute(string a)
     {
        //concrete implementation
     }
}

public class MyCoolChildTwo : MyCoolBase  
{
     public override void Execute(int a, int b)
     {
        //concrete implementation
     }
}

等等...

不好:當你在代碼中做這樣的事情時

MyCoolBase myCoolBase = new MyCoolChildOne ();

myCoolBase...?(); // should be really sure which function you're going to call on this line

好:你有強大的類型管理,沒有更多的object[]數組,或者你必須覆蓋的多個接口的多重繼承,相反在這種情況下你甚至可以避免它,即使我認為這不是一個好主意。

順便說一句,就像這里的極客所說,我認為您的架構不是很可靠,應該有其他一些解決方案適合您。 我們只是試圖找出代碼和問題的最佳選擇,但真正的問題只有你自己知道。

希望這可以幫助。

問候。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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