对泛型类型从抽象父类的List调用方法 [英] Calling a method on a List of Generic Type from abstract Parent class

查看:370
本文介绍了对泛型类型从抽象父类的List调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我当前的类型层次:

Here is my current Type Hierarchy:

我试图实施 PlaneRegion 的方法,将调用一个名为方法 Shift键()在它的派生类,其中的列表中所有的人都被称为PlaneBoundaries名单上,但它们是不同类型的。

I am trying to implement a method in PlaneRegion that will call a method named Shift() on a list in its derived classes where the list is called PlaneBoundaries in all of them but they are of different types

像这样:

public abstract class PlaneRegion<T>
{
    public abstract List<T> PlaneBoundaries { get; set; }
}


public class Polygon : PlaneRegion<LineSegment>
{
    public override List<LineSegment> PlaneBoundaries
    {
        get { return _planeBoundaries; }
        set { _planeBoundaries = value; }
    }
    protected List<LineSegment> _planeBoundaries;
}


public class NonPolygon : PlaneRegion<IEdge>
{
    public override List<IEdge> PlaneBoundaries
    {
        get { return _planeBoundaries; }
        set { _planeBoundaries = value; }
    }
    private List<IEdge> _planeBoundaries;

}



理想情况下它也应该返回对象的副本作为其。子类,不能修改原始对象

Ideally it should also return a copy of the object as its subclass and not modify the original object.

目前,我有接口IEdge由两个类实现:线段和圆弧。我使用泛型的抽象超PlaneRegion因为两个继承类,多边形和NonPolygon,都有planeBoundaries,而是一个多边形只包含直线(lineSegments),而NonPolygon可以直线或曲线(线段或弧),所以我实现像这个问题,你可以在下面的代码片段看到:的覆盖与派生类型和相同名称的C#

Currently, I have the Interface IEdge implemented by two classes: LineSegment and Arc. I am using Generics for the abstract superclass PlaneRegion because the two inheriting classes, Polygons and NonPolygon, both have planeBoundaries, but a Polygon only contains straight lines (lineSegments) while the NonPolygon can have straight or curved Lines(LineSegment or Arc) so I implemented like in this question as you can see in the snippets below: Override a Property with a Derived Type and Same Name C#

不过,由于PlaneRegion和PlaneRegion PlaneBoundaries是一个泛型类型它会导致问题的性质当我尝试呼吁PlaneBoundaries转变。下面是转变目前是如何实现的例子:

However, because PlaneRegion and PlaneBoundaries in PlaneRegion are a generic type it causes problems when I try to call shift on the PlaneBoundaries. Below are examples of how Shift is currently implemented:

//In PlaneRegion
public PlaneRegion<T> Shift(Shift inShift)
{
    //does not work because Shift isn't defined for type T
    this.PlaneBoundaries.Shift(passedShift); 
}

//in Polygon
public override Polygon Shift(Shift passedShift)
{
    return new Polygon(this.PlaneBoundaries.Shift(passedShift));
}

//In NonPolygon
public override NonPolygon Shift(Shift passedShift)
{
    return new NonPolygon(this.PlaneBoundaries.Shift(passedShift));
}



有没有办法来调用这样或限制在一个泛型列表转变T的可能性类,在编译时实施IEdge?我曾尝试使得PlaneRegion移位通用以及但是它也不能工作。

Is there a way to call shift on a Generic List like this or to limit the possibilities of T to classes that implement IEdge at compile time? I have tried making Shift in PlaneRegion a generic as well but it did not work either.

此外,理想的情况下,我希望它返回原来的对象的副本的子对象,并修改这些代替了原来的对象PlaneBoundaries的PlaneBoundaries,但没怎么做到这一点。

Also, Ideally I want it to return copies of the original objects as the child object and to modify the PlaneBoundaries on those instead of the original Objects PlaneBoundaries but do not how to do that.

推荐答案

您可以缩小你的 PlaneRegion 类只承认T中 IEdge 接口的实现:

You can narrow your PlaneRegion class to admit only implementations of IEdge interface in T:

public abstract class PlaneRegion<T> where T : IEdge
{
    public abstract List<T> PlaneBoundaries { get; set; }
}



此外,在功能,您可能希望将其应用到每一个项目列表,而不是整个列表,所以你应该将其更改为:

Also, in the Shift function, you may want to apply it to every item in the list, not the whole list, so you should change it to:

//In PlaneRegion
public PlaneRegion<T> Shift(Shift inShift)
{
    this.PlaneBoundaries.ForEach(x => x.Shift(passedShift)); 
}

这篇关于对泛型类型从抽象父类的List调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆