多态类中称为神经元的专门方法

时间:2019-08-02 13:14:33

标签: c# polymorphism

在使用多态性进行几何运算的代码中,我有几种专门的方法可用于处理相同类型的行为,但是只调用了父类方法。 这是总是在Bound类中调用的函数:

public override bool Intersect(SpatialData d)
{
    Type T = d.GetType();
    if (T == typeof(Vector)) return Intersect((Vector)d);
    if (T == typeof(Cell)) return Intersect((Cell)d);
    if (T == typeof(Bound)) return Intersect((Bound)d);
    if (T == typeof(Polygon)) return Intersect((Polygon)d);
    if (T == typeof(ConvexPolygon)) return Intersect((ConvexPolygon)d);
    if (T == typeof(Circle)) return Intersect((Circle)d);
    if (T == typeof(Edge)) return Intersect((Edge)d);
    throw new NotImplementedException();
}

上面的所有类(向量,单元格,绑定...)都从抽象SpatialData类继承。 所有Intersect变体都覆盖SpatialData中的抽象对应方法。 当然,这段代码很难看,但是如果我不这样做,则调用上面的方法而不是专门的方法:

public List<T> Intersect(SpatialData bbox)
{
    /*...*/
    if (!Root.Intersect(bbox)) return result;

其中bbox是Vector:SpatialData,而Root是Node:Bound和Bound:SpatialData

那么,如何使Root.Intersect(bbox)调用Bound.Intersect(Vector)而不是Bound.Intersect(SpatialData)?

希望我很清楚:s

0 个答案:

没有答案