我想我在这里遇到了一个基本的 OOP 误解:
(顺便说一句,这些是 Entity Framework 6 类,以防您对“虚拟”感到惊讶)
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; }
}
但这不是最好的解决方案,对吗? 我想确定受害者可以是或者一支部队或者一支人口。
我也考虑过通过 setter 来完成:
public ICollection<object> Victims { get;
set
{
if (value.GetType() == typeof(Troop) || value.GetType() == typeof(Population))
Victims.Add(value);
}
}
但我仍然不认为这是最好的方法,或者也许是...... 有没有更好、更干净的?
您可以使用接口。
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)
接口伙伴!
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;
}
}
您也可以使用 TypeConverter。