+-
CodeGo.net>我如何写两个参数都是接口的重载运算符
我的大多数东西都使用界面.我找不到创建重载运算符的方法,该方法允许我对实现IPoint接口的任何对象执行加法运算


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); 
   }
}

最佳答案
你不知道想象一下,如果您有两个IPoint实例a和b,它们都是不同的类(它们实现了IPoint).现在称为“ a b”.叫哪个接线员?返回哪种具体类型?

编辑:很抱歉,为了澄清您的评论,“按现状,不支持C#”.关于您是否应该能够执行此操作,我有一些哲学争论,因为我怀疑您可能会造成一些重大混乱.

点击查看更多相关文章

转载注明原文:CodeGo.net>我如何写两个参数都是接口的重载运算符 - 乐贴网