簡體   English   中英

C#中具有不同參數的多功能

[英]Multiple functions with different parameters in c#

我想請問什么是處理以下情況的最佳方法。

我想為具有不同參數的不同實例調用填充函數。 我在每個繼承類中實現了填充函數。 但是我不知道數百次使用它的最佳方法是什么。

(例如, 索引將是世界上的國家總數)。

public enum TCountry
{
   tAustralia,
   tUnitedKingdom,
   .. etc..
}
public enum TCity
{
   tSydney,
   tLondon,
   ... etc..
}
public int ProcessData ( string population, int index)
{
   switch (index)
   {
      case 0: 
         TypeAust aus = new TypeAust();
         retun aus.poulate( population, tAustralia, tSydney);
         // Different calculation Sydney -Aus
         DisplayAus(); // Display for Sydney - Aus

      case 1: 
         TypeUK uk = new TypeUK();
         retun uk.poulate( population, tUnitedKingdom, tLondon);
         // Different calculation for Londond - UK
         DisplayUK(); // Display for London - UK
      ....
      ... etc..
   }
}

提前致謝

我建議您使用其他設計。 無需使用該case語句,而是將所有類型放入集合中並調用填充,而無需知道您正在使用哪種特定類型。

 List<TypeCountry> countries = new List<TypeCountry>() { new TypeUK(), TypeAus(), TypeUS() //ect };
 //use this list all throughout the program

除了切換語句,您還可以執行;

  return countries[index].populate( //args go here );

然后,您可以做其他事情,例如:

  int worldPop = countries.Aggregate((c, n) => c.Populate(//args) + n.Populate(//args));

通常,您需要停止將每種類型視為與下一種不同。 將您的邏輯移到基類中,擺脫掉必須按名稱引用該類型的代碼,或者需要對其特定的繼承類型進行特定檢查。 在幾乎所有情況下,您都應該能夠擁有基類類型的集合,並將其傳遞並進行處理,而不必知道您要處理的特定實例是什么類型。 如果不是這種情況,則說明您繼承錯誤。 如果您有大寫的陳述,那么即使使用繼承也沒有任何意義,因為據我所知,您沒有獲得任何好處。

我將不得不看更多您的實際需求和體系結構,但您可以研究泛型。

public int ProcessData<T>(string population) where T : BaseCountry, new() {
    var country = new T();
    Display(country);
    return country.Populate(population);
}

public void Display<T>(T country) where T : BaseCountry { ... }

您將使用ProcessData像:

ProcessData<TypeAust>(99);

您的顯示方法也將是通用的。 這樣,您的過程數據方法始終被約束為可與實現BaseCountry的任何對象一起使用。 BaseCountry將定義一個抽象或虛擬Populate()方法。

您可以執行以下操作,以便將邏輯分為不同的類:

public enum TCountry
{
   tAustralia,
   tUnitedKingdom
}
public enum TCity
{
   tSydney,
   tLondon
}

public abstract class TypeCountry
{
    public abstract int Populate(string population);
}

public class TypeAust : TypeCountry
{
    public override int Populate(string population)
    {
        // do your calculation with tAustralia, tSydney...
    }
}

public class TypeUK: TypeCountry
{
    public override int Populate(string population)
    {
        // do your calculation with tUnitedKingdom, tLondon...
    }
}

public static class TypeCountryFactory
{
    public static TypeCountry GetCountry(TCountry country)
    {
        switch (country)
        {
            case TCountry.tAustralia:
                return new TypeAust();
            case TCountry.tUnitedKingdom:
                return new TypeUK();
        }
    }
}

public int ProcessData (string population, int TCountry)
{
    TypeCountry country = TypeCountryFactory.GetCountry(TCountry);

    return country.Populate(population);
}

暫無
暫無

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

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