class TypeA
{
public TypeA Copy() { ... }
public bool IsEqual(TypeA mytypeA) { ... }
public bool IsSame(TypeA mytypeA) { ... }
...
}
class TypeACollection : List<TypeA>
{
public bool IsSame (TypeACollection typeAs)
{
if (Count != typeAs.Count)
return false;
return this[0].IsSame(typeAs[0]);
}
...
}
TypeB,TypeC与TypeA有相似的功能Copy/IsEqual/IsSame。 TypeBollection,TypeCollection有类似的IsSame。 TypeBCollection使用TypeB.IsSame,TypeCCollection使用TypeC.IsSame。通用?抽象类?什么是最好的设计我的代码?
现在,我计划增加2个新的类别:LocData和LocDataCollection
class LocData
{
public virtual TypeA Copy() { ... }
public virtual bool IsEqual(TypeA mytypeA) { ... }
public virtual bool IsSame(TypeA mytypeA) { ... }
...
}
class LocDataCollection<T> : List<LocData> where T: LocData
{
public bool IsSame (LocDataCollection<T> typeAs)
{
if (Count != typeAs.Count)
return false;
return this[0].IsSame(typeAs[0]);
}
...
}
和重写现有的代码,
class TypeA : LocData
{
public new TypeA Copy() { ... }
public new bool IsEqual(TypeA mytypeA) { ... }
public new bool IsSame(TypeA mytypeA) { ... }
...
}
class TypeACollection : ???
{
???
// so I can remove IsSame here,
// when I call IsSame, it will use one from LocDataCollection and still call
// TypeA.IsSame
...
}
现在我失去了在抽象/虚拟/通用/ ..这是最好的方法吗?
'IsEqual'和'IsSame'有什么区别? – 2012-04-19 15:46:13
你应该继承'Collection',而不是'List '。 –
SLaks
2012-04-19 15:46:58
IsEqual检查密钥是否相等,IsSame检查内容是否相同 – weslleywang 2012-04-19 17:57:14