嗯,根据我的经验和 Oracle Java API 文档,我知道他们这样做,但我想知道为什么。
通过构造函数,我只允许将 int
类型的参数传递给 Rectangle
类,x、y 等的内部数据表示是 类型int
和 setSize()
仅排除 int
类型的参数。
但是,为什么所有方法,如 getX()
、getY()
、getWidth()
等,在没有精确?为什么不像预期的那样简单 int
?
编辑:
我知道它是从 Rectangle2D
类派生的,但是仍然没有理由不提供任何基于 int
的 getX()
和 getY()
函数,与 Point
和 Point2D
类不同,这些方法不是抽象的。此外,setLocation()
也不是抽象的。
最佳答案
我相信因为它扩展了Rectangle2D在 Graphics2D 中使用,它说:
This class is only the abstract superclass for all objects that store a 2D rectangle. The actual storage representation of the coordinates is left to the subclass.
因此,如果它的任何子类表示 double
中的维度,也许应该彼此兼容。如果您阅读 Rectangle#getX() 的 Javadoc :
Returns the X coordinate of the bounding Rectangle in double precision.
Specified by:
public abstract double getX() in class RectangularShape.
关于java - 矩形类函数 getX()、getY() 等以 double 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17400840/