Use generics instead of inheritance:
class Triangle<T> where T: ILine
{
List<T> edges;
···
}
and then instantiate it as
Triangle<Line>
or
Triangle<SpecialLine>
سؤال
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:
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?
المحلول
Use generics instead of inheritance:
class Triangle<T> where T: ILine
{
List<T> edges;
···
}
and then instantiate it as
Triangle<Line>
or
Triangle<SpecialLine>