不同子类的相同方法
Same Method for different Subclasses
我有一个 C#
-Class Point
有两个子类 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
}
现在我想要两件事。
我想要一个方法 AdaptMeshPoints
接受 ColorPoint
和 AmountPoint
输入列表,我可以更改两者的公共参数(它们是Point
)
中的参数
我想告诉方法AdaptMeshPoints
,它应该打印出子类的哪个参数。
这应该看起来像这样:
public class main
{
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AdaptMeshPoints<ColorPoint>(colorList, color);
}
public List<var> AdaptMeshPoints<var>(List<var> pointList, varType whatToPrint)
{
pointList[0].x = 45;
Console.WriteLine(pointList[0].whatToPrint);
}
}
我假设这是来自你问题文本的 C#,即使你的问题同时标记有 C# 和 Java。
为了能够设置 pointList[0].x
,您需要告诉编译器 T
将始终是 Point
(或继承自它的东西)。使用泛型类型约束 (where T : Point
) 执行此操作。
您还可以传递一个描述您要打印的 属性 的委托:
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AdaptMeshPoints(colorList, x => x.color.ToString());
}
public List<T> AdaptMeshPoints<T>(List<T> pointList, Func<T, string> whatToPrint)
where T : Point
{
pointList[0].x = 45;
Console.WriteLine(whatToPrint(pointList[0]));
}
到 1. 创建一个接受 Point[] 的函数。如果存在 "no danger of Data loss" 并且这种多态情况适用于这种情况,则会自动进行转换。
(我不是 100% 确定这是多态性还是更多地属于协方差和反方差领域。但是这里的规则有意非常相似)。
到2. 这个至少可以通过以下方式解决:
- 一个额外的参数和一个switch/case块
- 将函数交给另一个函数或接口来保存访问变量的实际代码
- 将代码交给 Lambda 或匿名函数来完成实际工作。
所以你必须"pick your Posion"。
我有一个 C#
-Class Point
有两个子类 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
}
现在我想要两件事。
我想要一个方法
AdaptMeshPoints
接受ColorPoint
和AmountPoint
输入列表,我可以更改两者的公共参数(它们是Point
) 中的参数
我想告诉方法
AdaptMeshPoints
,它应该打印出子类的哪个参数。
这应该看起来像这样:
public class main
{
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AdaptMeshPoints<ColorPoint>(colorList, color);
}
public List<var> AdaptMeshPoints<var>(List<var> pointList, varType whatToPrint)
{
pointList[0].x = 45;
Console.WriteLine(pointList[0].whatToPrint);
}
}
我假设这是来自你问题文本的 C#,即使你的问题同时标记有 C# 和 Java。
为了能够设置 pointList[0].x
,您需要告诉编译器 T
将始终是 Point
(或继承自它的东西)。使用泛型类型约束 (where T : Point
) 执行此操作。
您还可以传递一个描述您要打印的 属性 的委托:
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AdaptMeshPoints(colorList, x => x.color.ToString());
}
public List<T> AdaptMeshPoints<T>(List<T> pointList, Func<T, string> whatToPrint)
where T : Point
{
pointList[0].x = 45;
Console.WriteLine(whatToPrint(pointList[0]));
}
到 1. 创建一个接受 Point[] 的函数。如果存在 "no danger of Data loss" 并且这种多态情况适用于这种情况,则会自动进行转换。
(我不是 100% 确定这是多态性还是更多地属于协方差和反方差领域。但是这里的规则有意非常相似)。
到2. 这个至少可以通过以下方式解决:
- 一个额外的参数和一个switch/case块
- 将函数交给另一个函数或接口来保存访问变量的实际代码
- 将代码交给 Lambda 或匿名函数来完成实际工作。
所以你必须"pick your Posion"。