如何编写两个参数都是接口的重载运算符
我的大部分东西都使用接口。 我找不到创建重载运算符 + 的方法,该运算符允许我对实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不知道。 想象一下,如果您有两个 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.
我不认为你能做到你所要求的,我回顾过去却一无所获。 问题是,即使在使用
.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 themovement
parameter to aPoint 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
inMoveOf
then it would be safe to always cast it toPoint
inside the method, however if this is the case then you should be acceptingPoint
as the parameter.The only other thing I can say is that if you are creating a
Point Type
initially and passing that toMoveOf
, you could check the type ofmovement
insideMoveOf
before casting it. The problem here of course is that you would eventually have to do this for all types that inherit fromIPoint
and use theMoveOf
method.