创建变量类型的实例 'T'
Create an instance of the variable type 'T'
我有一个 C#
-Class Point
有两个 Subclasses ColorPoint
和 AmountPoint
.
点-Class
public class Point
{
double x; // Position x
double y; // Position y
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
}
现在在我的 main
class 我想在列表中创建一个新点
这应该看起来像这样:
public class main
{
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public List<T> AddPoint<T>(List<T> pointList)
where T : Point
{
pointList.Add(new T(0, 0)); // DOES NOT WORK (Cannot create instance of variable type 'T')
pointList.Add(new Point(0, 0)); // DOES NOT WORK (Cannot convert Point to T)
}
}
变量color
或amount
在这两种情况下都可以保留为null
。
您的代码无法编译。我无法想象为什么你会想要做你想做的事。但最接近合法实施的是:
class Program
{
static void Main(string[] args)
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public static List<T> AddPoint<T>(List<T> pointList)
where T : Point, new()
{
pointList.Add(new T());
return pointList;
}
}
public class Point
{
double x; // Position x
double y; // Position y
public Point() : this(0, 0)
{
}
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
public ColorPoint()
{
}
public ColorPoint(double pos_x, double pos_y) : base(pos_x, pos_y)
{
}
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
public AmountPoint()
{
}
public AmountPoint(double pos_x, double pos_y) : base(pos_x, pos_y)
{
}
}
我有一个 C#
-Class Point
有两个 Subclasses ColorPoint
和 AmountPoint
.
点-Class
public class Point
{
double x; // Position x
double y; // Position y
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
}
现在在我的 main
class 我想在列表中创建一个新点
这应该看起来像这样:
public class main
{
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public List<T> AddPoint<T>(List<T> pointList)
where T : Point
{
pointList.Add(new T(0, 0)); // DOES NOT WORK (Cannot create instance of variable type 'T')
pointList.Add(new Point(0, 0)); // DOES NOT WORK (Cannot convert Point to T)
}
}
变量color
或amount
在这两种情况下都可以保留为null
。
您的代码无法编译。我无法想象为什么你会想要做你想做的事。但最接近合法实施的是:
class Program
{
static void Main(string[] args)
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public static List<T> AddPoint<T>(List<T> pointList)
where T : Point, new()
{
pointList.Add(new T());
return pointList;
}
}
public class Point
{
double x; // Position x
double y; // Position y
public Point() : this(0, 0)
{
}
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
public ColorPoint()
{
}
public ColorPoint(double pos_x, double pos_y) : base(pos_x, pos_y)
{
}
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
public AmountPoint()
{
}
public AmountPoint(double pos_x, double pos_y) : base(pos_x, pos_y)
{
}
}