저는 대부분 내 물건에 대해 인터페이스를 사용하고 있습니다. 내가 과부하 연산자를 만들 수있는 방법을 찾을 수 없습니다 + 그 날 코드두 인수가 모두 인터페이스 인 오버로드 연산자를 작성하는 방법
interface IPoint
{
double X { get; set; }
double Y { get; set; }
}
class Point : IPoint
{
double X { get; set; }
double Y { get; set; }
//How and where do I create this operator/extension ???
public static IPoint operator + (IPoint a,IPoint b)
{
return Add(a,b);
}
public static IPoint Add(IPoint a,IPoint b)
{
return new Point { X = a.X + b.X, Y = a.Y + b.Y };
}
}
//Dumb use case :
public class Test
{
IPoint _currentLocation;
public Test(IPoint initialLocation)
{
_currentLocation = intialLocation
}
public MoveOf(IPoint movement)
{
_currentLocation = _currentLocation + intialLocation;
//Much cleaner/user-friendly than _currentLocation = Point.Add(_currentLocation,intialLocation);
}
}
지금 C#에서이를 수행 할 방법이 없습니다. 우리는이 기능을 C# 4.0에 "확장 연산자"형태로 추가하는 것을 고려했지만, 충분히 우선 순위가 높은 기능은 아니며 잘려나갔습니다. 죄송합니다! –
고마워, 내 질문에 대답 해! –