为什么java.awt.Point中提供了一些方法来设置和获取双打,但商店X和Y为int的? [英] Why does java.awt.Point provide methods to set and get doubles but store x and y as int's?
问题描述
As you can see in the Oracle Documentation for java.awt.Point, x and y are stored as int
. However, getX
and getY
return double
. While there is a setLocation
method that takes 2 double
types, there is no constructor that does. Furthermore, the double
gets truncated to an int
internally anyway.
是否有一个很好的理由?你可能会避免在的setLocation
铸造由具有采取的方法双击
的类型,但你必须当你相反的问题叫的getX
和的getY
。还有Point类的precision的misre presentation通过返回双击
从的getX
和的getY
。
Is there a good reason for this? You might avoid a cast on setLocation
by having a method that takes double
types, but you have the opposite problem when you call getX
and getY
. There's also a misrepresentation of the precision of the Point class by returning a double
from getX
and getY
.
推荐答案
由于它java.awt.geom.Point2D中这是在Graphics2D的抽象类使用的延伸。因为在此之前有人以这种方式实现的兼容性原因,所有的方法都INT基于Java只支持java.awt.Graphics。
Because it extends from java.awt.geom.Point2D which is used in Graphics2D abstract class. It was implemented this way for compatibility reason since before this, java only supported java.awt.Graphics where all methods are int based.
这篇关于为什么java.awt.Point中提供了一些方法来设置和获取双打,但商店X和Y为int的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!