0
  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 功能。TypeBCollection、TypeACollection 有类似的 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
    ...
}

现在我迷失在抽象/虚拟/通用/...,这是最好的方法?

4

1 回答 1

1

您应该替换newoverrides,然后创建一个LocDataCollection<T> : Collection<T> where T : LocData.

于 2012-04-19T15:47:50.087 回答