问题描述
目前,我使用Rectangle和Rectangle2D(Rectangle2D.Double)交换。
Currently, I use Rectangle and Rectangle2D (Rectangle2D.Double) interchange.
我想知道在选择正确的数据类型时需要考虑的是什么?我没有看到任何明显的区别,除了Rectangle(Rectangle2D的子类)有更多的API函数。
I was wondering what is the consideration I shall take in, while choosing the correct data type? I do not see any obvious difference, except the Rectangle (sub class of Rectangle2D) are having more API functions.
谢谢。
推荐答案
Rectangle使用int坐标。
Rectangle2D是一个抽象类,它不关心你是使用int,double还是float坐标。
Rectangle uses int coordinates. Rectangle2D is an abstract class that doesn't care if you're using int, double or float coordinates.
如果你需要更高精度的double和浮点数,你必须使用Rectangle2D。
If you need the greater precision of double and float, you'll have to go with Rectangle2D.
Rectangle2D是基类,所以如果你正在编写以抽象方式对矩形形状进行操作的代码,那么去吧对于Rectangle2D,并像这样分配:
Rectangle2D is the base class, so if you're writing code that operates on rectangular shapes in an abstract way, go for Rectangle2D, and assign it like so:
Rectangle2D rect = new Rectangle2D.Double(double, double, double, double);
或
Rectangle2D rect = new Rectangle(int, int, int, int)
如果你知道的话你只打算处理整数,你可以一直使用矩形。
If you know you're only going to deal with ints, you can use Rectangle all the way.
你可能会说Rectangle应该叫做Rectangle2D.Integer。但那也不是,因为例如Rectangle是实现可序列化接口的三个中唯一的一个。
You might say that Rectangle should be called Rectangle2D.Integer. But that's not quite it either, because e.g. Rectangle is the only one of the three that implements the serializable interface.
像skaffman评论的那样,这是一个遗留问题。
Like skaffman commented, it's a bit of a legacy issue.
这篇关于Rectangle和Rectangle2D的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!