내가 정확한 문제는 당신이 가지고있는,하지만 여기가 나를 위해 일한 방법은 무엇인지 확실하지 않다 :.
먼저 실제 클래스 구현 :
public interface IPoint
{
int X { get; set; }
int Y { get; set; }
}
public class Point : IPoint
{
public int X { get; set; }
public int Y { get; set; }
public Point()
{
}
public Point(int x, int y)
{
X = x;
Y = y;
}
}
public class GazePoint : IPoint
{
public int X { get; set; }
public int Y { get; set; }
public GazePoint()
{
}
public GazePoint(int x, int y)
{
X = x;
Y = y;
}
}
그런 다음 실제 AvgPo INT 메소드 구현 : 마지막으로
public static Point AvgPoint(IEnumerable<IPoint> locations)
{
if (locations == null || !locations.Any()) return new Point(0, 0);
return new Point((int) locations.Average(l => l.X),
(int) locations.Average(l => l.Y));
}
그리고 몇 가지 검사 : 당신은 같은 것이 일반적인 할 수
당신의 목표는 방법에 전달 된 동일한 유형의 평균을 반환해야하는 경우
public static void Main()
{
var points = new List<Point>
{
new Point(1, 2),
new Point(3, 4)
};
var gazePoints = new List<GazePoint>
{
new GazePoint(1, 2),
new GazePoint(3, 4)
};
Point avgPoint = AvgPoint(points);
Point avgGazePoint = AvgPoint(gazePoints);
Console.WriteLine("Average Point = {0}, {1}", avgPoint.X, avgPoint.Y);
Console.WriteLine("Average GazePoint = {0}, {1}", avgGazePoint.X, avgGazePoint.Y);
}
, 그래서 :
public static T AvgPoint<T>(IEnumerable<T> locations) where T : IPoint, new()
{
if (locations == null || !locations.Any()) return new T {X = 0, Y = 0};
return new T {X = (int) locations.Average(l => l.X),
Y = (int) locations.Average(l => l.Y)};
}
'AvgPoint' 메소드에는 return 문이 없습니다. 실제 문제를 나타내는 샘플 코드를 게시하십시오. –
'작동하지 않는 것 '을 정의 할 수 있습니까? 예외 또는 오류? –
메서드 서명이 정상적으로 작동합니다. 나머지는 어떻게 구현하고 있습니까? –