Java Point,getX() 和 point.x 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10221327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:06:10  来源:igfitidea点击:

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

javapoint

提问by James Fazio

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

我很困惑,为什么 Java Point 类接受两个 int 参数,而 getX() 和 getY() 方法返回双精度值。例如我可以定义一个 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

它将返回 4.0。如果我打电话

p.x;

I would get 4.

我会得到 4。

Any reason for this?

这有什么原因吗?

采纳答案by trutheality

There are Point2D.Doubleand Point2D.Floatclasses that extend Point2Dwhich is a superclass of Pointand they need to be able to work with floating point values. Note that there is also a setLocation( double, double ).

Point2D.DoublePoint2D.Float扩展类Point2D是一个超类Point,他们需要能够将工作与浮点值。请注意,还有一个setLocation( double, double ).

Point2Dis an abstract class that implements the distance calculation for points, and setLocation, getX, and getYare its abstract methods, which is why they all use doublesand why Pointhas to implement them with doubles in the signature.

Point2D是一个抽象类,它实现的距离计算点,和setLocationgetXgetY是它的抽象方法,这就是为什么他们都用doubles,为什么Point有执行这些doubleS IN的签名。