2016-09-09 2 views
1

のプロパティのC#(複数)を定義可能なタイプ:私はここに誤解基本的なOOPを経験していると思うクラス

(これらは、場合にあなたが「仮想に驚いている、ところで6クラスEntity Frameworkのです")

public class WeaponUsed : HistoryEvent 
{ 
    public virtual Player Target { get; set; } 
    public virtual GameCountry Country { get; set; } 
    //Victims: Troops or Pops 
    public ICollection<Victim> Victims { get; set; } 
    public bool HasMoreWeaponsLeft { get; set; } 
} 

犠牲者は、"兵士 "オブジェクトまたは"人口 "オブジェクトになります。犠牲者クラスはどのように見えますか? 2つのプロパティを使用し、未使用のものを「null」に設定することができます。

public class Victim 
{ 
    public virtual Troop TroopVictim { get; set; } 
    public virtual Population PopVictim { get; set; } 
} 

しかし、これは最善の解決策ではありませんか? 私は被害者がであると判断したいと思います a troop または人口です。

私はまた、セッターを経由して、それをやって考えた:

public ICollection<object> Victims { get; 
    set 
    { 
     if (value.GetType() == typeof(Troop) || value.GetType() == typeof(Population)) 
      Victims.Add(value); 
    } 
} 

しかし、私はまだそれが最善の方法だとは思わない、または多分それは... 良く、クリーナー1はありますか?

+0

'enum victim {Troop、Population}'のように 'enum'を試してみることができます。 –

+0

私は知っていますが、私はオブジェクトに格納されている余分な情報が必要です。 "名前"自体は十分ではありません。 – Tornister

+0

どのような追加情報を保存する必要がありますか? –

答えて

1

インターフェイスを使用できます。

public class WeaponUsed : HistoryEvent 
{ 
    public virtual Player Target { get; set; } 
    public virtual GameCountry Country { get; set; } 
    //Victims: Troops or Pops 
    public IVictim Victims { get; set; } 
    public bool HasMoreWeaponsLeft { get; set; } 
} 

public interface IVictim { 
    // common methods and properties for PopVictim and TroopVictim 
    int Number {get;} 
} 

public class TroopVictim : IVictim { 
    // TroopVictim will be enforced to have IVictim methods and proprieties 
    public int Number{ get {return 1; } } 
} 

public class PopVictim : IVictim { 
    // PopVictim will be enforced to have IVictim methods and proprieties 
    public int Number{ get {return 100; } } 
} 

使用法:

Console.WriteLine(weapon.Victims.Number) 
+0

私の主な要件は、関連する軍隊/人口オブジェクトへの接続/外部キーを維持することでした。ここで私はちょうど新しいものを作ります。それは私の問題で私と一緒に私を助けません。私は犠牲者からTroop/Populationが継承されるようにしなければならないと思っています。何か誤解していますか? – Tornister

0

インタフェースメイト!

public interface IVictim 
    { 
     string SharedProp { get; set; } 
    } 

    public class TroopVictim : IVictim 
    { 
     public string SharedProp { get; set; } 

     public string TroopProperty { get; set; } 
    } 

    public class PopVictim : IVictim 
    { 
     public string SharedProp { get; set; } 

     public string PopProperty { get; set; } 
    } 

    public class MyGenericVictim 
    { 
     //Optional Property 
     public bool? IsTroopVictim 
     { 
      get 
      { 
       if (Victim == null) return null; 
       return Victim.GetType() == typeof(TroopVictim); 
      } 
     } 

     public IVictim Victim { get; set; } 
    } 


    public class UseMyOfVictim 
    { 
     public void Bar() 
     { 
      Foo(new MyGenericVictim 
      { 
       Victim = new TroopVictim(), 
      }); 
     } 

     public void Foo(MyGenericVictim myvic) 
     { 
      //Function doesnt know TroopVic or PopVic 

      TroopVictim resolvedTroopVic; 
      PopVictim resolvedPopVictim; 

      if (myvic.Victim.GetType() == typeof(TroopVictim)) 
      { 
       resolvedTroopVic = (TroopVictim) myvic.Victim; 
      } 
      else if (myvic.Victim.GetType() == typeof(PopVictim)) 
      { 
       resolvedPopVictim = (PopVictim) myvic.Victim; 
      } 

      string success = resolvedPopVictim.PopProperty; 
      success = resolvedTroopVic.TroopProperty; 
     } 
    } 
+0

私の主な要件は、関連する軍隊/人口オブジェクトへの接続/外部キーを維持することでした。ここで私はちょうど新しいものを作ります。それは私の問題で私と一緒に私を助けません。私は犠牲者からTroop/Populationが継承されるようにしなければならないと思っています。何か誤解していますか? – Tornister

関連する問題