Java Point, difference between getX() and point.x

☆樱花仙子☆ 提交于 2019-12-09 03:13:54

问题


I am confused to as why the Java Point class takes in two int paramaters and the getX() and getY() methods return doubles. For example I could define a Point

Point p = new Point(4,6);

If I were to call..

p.getX();

It would return 4.0. and if I were to call

p.x;

I would get 4.

Any reason for this?


回答1:


There are Point2D.Double and Point2D.Float classes that extend Point2D which is a superclass of Point and they need to be able to work with floating point values. Note that there is also a setLocation( double, double ).

Point2D is an abstract class that implements the distance calculation for points, and setLocation, getX, and getY are its abstract methods, which is why they all use doubles and why Point has to implement them with doubles in the signature.



来源:https://stackoverflow.com/questions/10221327/java-point-difference-between-getx-and-point-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!