如何编写两个参数都是接口的重载运算符

发布于 2024-07-21 20:51:11 字数 922 浏览 5 评论 0原文

我的大部分东西都使用接口。 我找不到创建重载运算符 + 的方法,该运算符允许我对实现 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); 
   }
}

I'm using interface for most of my stuff. I can't find a way to create an overload operator + that would allow me to perform an addition on any objects implementing the IPoint interface

Code


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-07-28 20:51:11

你不知道。 想象一下,如果您有两个 IPoint 实例,a 和 b,它们都是不同的类(实现 IPoint)。 现在调用“a + b”。 哪个操作员+被调用? 返回哪种具体类型?

编辑:抱歉,澄清一下你的评论,“不是在 C# 中”。 我对你是否应该能够做到这一点进行了一些哲学辩论,因为我怀疑你可能会造成一些严重的混乱。

You don't. Imagine if you have two IPoint instances, a and b, both of different classes (which implement IPoint). Now call "a + b". Which operator+ gets called? Which concrete type is returned?

Edit: Sorry, to clarify to your comment, "Not in C# as it stands". I have some philipsophical debate as to whether you should even be able to do this, as I suspect you can create some significant confusion.

趁微风不噪 2024-07-28 20:51:11

我不认为你能做到你所要求的,我回顾过去却一无所获。 问题是,即使在使用 .Add 的示例中,您仍然需要将 movement 参数转换为 Point Type 才能访问到那个方法。 我相信这是 C# 的限制,如果我错了,我会很高兴知道它。

我建议的是,如果您知道,您总是会在MoveOf中获得Point Type,那么它在方法内始终将其强制转换为 Point 是安全的,但是如果是这种情况,那么您应该接受 Point 作为参数。

我唯一能说的是,如果您最初创建一个 Point Type 并将其传递给 MoveOf,您可以检查 movement 的类型> 在投射之前在 MoveOf 内。 当然,这里的问题是您最终必须对从 IPoint 继承并使用 MoveOf 方法的所有类型执行此操作。

I don't think you can do what you're asking, I've looked in the past and come up empty. The problem is, even in your example of using .Add is that you still need to cast the movement parameter to a Point Type to have access to that method. This, I believe is a limitation of C#, if I'm wrong I'd be very happy to know about it.

What I would suggest is that if you know you are always going to get a Point Type in MoveOf then it would be safe to always cast it to Point inside the method, however if this is the case then you should be accepting Point as the parameter.

The only other thing I can say is that if you are creating a Point Type initially and passing that to MoveOf, you could check the type of movement inside MoveOf before casting it. The problem here of course is that you would eventually have to do this for all types that inherit from IPoint and use the MoveOf method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文