我有两个具有相同名称的函数,主要区别在于不同的返回类型.我怎么能重载函数才能使用相同的名称,因为有时我需要point3d或point3f数组,以下函数名称给出相同的错误:
public static Point3d[] GetNGonCenters(this Mesh mesh)
{
Point3d[] centers = new Point3d[mesh.Ngons.Count];
for (int i = 0; i < mesh.Ngons.Count; i++)
centers[i] = mesh.Ngons.GetNgonCenter(i);
return centers;
}
public static Point3f[] GetNGonCenters(this Mesh mesh)
{
Point3f[] centers = new Point3f[mesh.Ngons.Count];
for (int i = 0; i < mesh.Ngons.Count; i++)
centers[i] = (Point3f)mesh.Ngons.GetNgonCenter(i);
return centers;
}
Run Code Online (Sandbox Code Playgroud)
编译器无法知道你在调用什么.我建议使名称更具描述性:
public static Point3d[] GetNGonCenters3D(this Mesh mesh)
Run Code Online (Sandbox Code Playgroud)
和
public static Point3f[] GetNGonCenters3F(this Mesh mesh)
Run Code Online (Sandbox Code Playgroud)
重载在这里不起作用,因为它们都使用相同的参数,编译器无法猜出你想要的返回类型.