简体   繁体   English

简单数学运算符上的奇怪Java错误

[英]Strange java error on simple mathematical operator

Maybe I'm crazy, but I want to incorporate some logic I learned in my XNA Game Development class into android/java game programming. 也许我疯了,但是我想将我在XNA Game Development类中学到的一些逻辑结合到android / java游戏编程中。 For example, I'm using Point rather than Vector2 to manipulate the location of graphics programmatically, and adjust graphic size based on an Android device's dpi and orientation. 例如,我使用Point而不是Vector2来以编程方式操纵图形的位置,并根据Android设备的dpi和方向调整图形大小。

I thought finding the center of the graphic would be a great asset when laying out the GUI and determining which folder (hdpi, ldpi, or mdpi) the graphics should be taken from. 我认为在布局GUI并确定应从中提取图形的文件夹(hdpi,ldpi或mdpi)时,找到图形的中心将是一项巨大的资产。 Unfortunately, Eclipse seems to have a problem with the plus sign. 不幸的是,Eclipse似乎对加号有疑问。 This is interesting because I am trying to perform a simple mathematical operation.: 这很有趣,因为我试图执行一个简单的数学运算。

The operator + is undefined for the argument type(s) android.graphics.Point, android.graphics.Point 未为参数类型android.graphics.Point,android.graphics.Point定义运算符+

//Java  code was both copied & pasted as well as written to make sure Visual Studio
public Point Center()
{
    return new Point(_frameWidth/2, _frameHeight/2)+_location;
}

//Original XNA code
//Determines the coordinates of a sprite's center
public Vector2 Center
{
    get
    {
        return _location + new Vector2(_frameWidth / 2, _frameHeight / 2);
    }
}

And it's true; 这是真的。 you can't add arbitrary types in Java--the error message is correct. 您不能在Java中添加任意类型-错误消息是正确的。

You get some syntactic sugar for string concatenation, and that's it--anything else you add had better be a primitive or mappable object wrapper for the same (eg, I'm looking at you, BigDecimal , no + ). 您会得到一些用于字符串连接的语法糖,就是这样-您添加的其他内容最好都是相同的原始或可映射对象包装器(例如,我在看着您, BigDecimal ,no + )。

Whatever _location is, if you're trying to add it to a Point or Vector2 , it won't work using + . 不管_location是什么,如果您尝试将其添加到PointVector2 ,都无法使用+起作用。 You may add individual components, use vector/point methods or utility libraries, etc. 您可以添加单个组件,使用矢量/点方法或实用程序库等。

It's not an issue with Eclipse; Eclipse没问题; Java does not have operator overloading (besides for a very small set of cases). Java没有运算符重载(除了极少数情况)。

Why doesn't Java offer operator overloading? Java为什么不提供运算符重载?

An interesting article on this: 一篇有趣的文章:

http://java.dzone.com/articles/why-java-doesnt-need-operator http://java.dzone.com/articles/why-java-doesnt-need-operator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM