简体   繁体   English

在抽象 class 构造函数中使用泛型类型

[英]Use generic type in abstract class constructor

I have a problem similar to this thread but mine is a bit different.我有一个类似于这个线程的问题,但我的有点不同。

I want to create something like this我想创造这样的东西

public abstract class Plot
{
    protected GameObject plotModel;
    protected IDataPoint[] data;
    protected GameObject[] plotModelInstances;

    protected Plot<TDataPoint>(TDataPoint[] data, GameObject plotModel, Vector2 plotCenter = default) where TDataPoint : IDataPoint
    {
        this.data = data;
        this.plotModel = plotModel;
        plotModelInstances = new GameObject[data.Length];
        this.plotCenter = plotCenter;
    }
}

A base class that takes in a data array of a generic type which implements the interface IDataPoint.一个基础 class 接收一个实现接口 IDataPoint 的泛型数据数组。 The child class is now supposed to be constructed with a data array of a struct that implements this interface子 class 现在应该使用实现此接口的结构的数据数组来构造

public BarPlot(BarDataPoint[] data, GameObject plotModel, float barWidth = 1, float barHeight = 1, Vector2  = default) : base(data, plotModel, plotCenter) 
    {
        this.barWidth = barWidth;
        this.barHeight = barHeight; 
    }

One answer in the thread linked above said that constructors can't use generics in C# and suggested a combination of a generic class and a static class. One answer in the thread linked above said that constructors can't use generics in C# and suggested a combination of a generic class and a static class. However, I don't want a whole class but only one parameter to be generic.但是,我不想要整个 class 而是只有一个参数是通用的。 Any ideas how to achieve that?任何想法如何实现这一目标?

Your best option is probably something like this:您最好的选择可能是这样的:

public abstract class Plot<TDataPoint>  where TDataPoint : IDataPoint
{
    protected GameObject plotModel;
    protected TDataPoint[] data; // Note: Changed IDatePoint[] to TDataPoint[]!
    protected GameObject[] plotModelInstances;

    // Note: Changed IDatePoint[] to TDataPoint[]!
    protected Plot(TDataPoint[] data, GameObject plotModel, Vector2 plotCenter = default)
    {
        this.data = data;
        this.plotModel = plotModel;
        plotModelInstances = new GameObject[data.Length];
        this.plotCenter = plotCenter;
    }
}

And then, in the child class:然后,在子 class 中:

public class BarPlot : Plot<BarDataPoint>
{

    public BarPlot(BarDataPoint[] data, GameObject plotModel, float barWidth = 1, float barHeight = 1, Vector2  = default) 
        : base(data, plotModel, plotCenter) 
    {
        this.barWidth = barWidth;
        this.barHeight = barHeight; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 继承需要构造函数的泛型类型的抽象类 - Abstract class inheriting a generic type requiring a constructor 在类构造函数中使用泛型类型 - Use generic type in class constructor 抽象类,在泛型方法中使用继承者的类型 - Abstract class, use type of inheritor in generic method 使用抽象 class 作为泛型类型 - Using abstract class as a generic type 获取通用抽象类的默认构造函数 - Getting the default constructor of a generic abstract class T必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“TModel” - T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method 通用 Class 加载程序:“T”必须是具有公共无参数构造函数的非抽象类型 - Generic Class Loader: 'T' must be a non-abstract type with a public parameterless constructor 没有[XMLInclude]或Type []构造函数的抽象类的序列化 - Serialization of an abstract class without [XMLInclude] or Type[] Constructor 抽象类的构造函数请求返回类型 - Abstract class' constructor asks for a return type 类型参数化抽象类的C#构造函数 - C# constructor for type parameterized abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM