Question

I am working in C# and I'm trying to write an AbstractTriangle hierarchy with two subclasses: Triangle and SpecialTriangle.

Triangles are made up of Lines, but SpecialTriangles are made up of SpecialLines!

I can think of a number of ways to store the lines in the triangles:

  1. AbstractTriangle contains a list of AbstractLine
  2. AbstractTriangle contains a list of ILine
  3. ITriangle has accessors for a list of ILine, Triangle and SpecialTriangle are responsible for their own lists of Line or SpecialLine

What is the correct, most elegant way of handling these hierarchies?

Do I need to sacrifice the triangles returning concrete typed lines to make this work properly?

Était-ce utile?

La solution

Use generics instead of inheritance:

class Triangle<T> where T: ILine
{
    List<T> edges;
    ···
}

and then instantiate it as

Triangle<Line> 

or

Triangle<SpecialLine>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top