我很惊讶地看到,height
和width
成员的获得者有return
类型double
,尽管它们是int
。此外,setSize
方法双参数的定义如下:java.awt的Dimension类中的方法返回类型
/**
* Sets the size of this <code>Dimension</code> object to
* the specified width and height in double precision.
* Note that if <code>width</code> or <code>height</code>
* are larger than <code>Integer.MAX_VALUE</code>, they will
* be reset to <code>Integer.MAX_VALUE</code>.
*
* @param width the new width for the <code>Dimension</code> object
* @param height the new height for the <code>Dimension</code> object
*/
public void setSize(double width, double height) {
this.width = (int) Math.ceil(width);
this.height = (int) Math.ceil(height);
}
请看看Dimension类。以上评论说,值不能超出Integer.MAX_VALUE。为什么? 为什么我们有double
?有没有什么微妙的原因?任何人都可以向我解释这个吗?对我的坚持抱歉!
那就是它?为什么我们有'double'作为'return'类型的getter?有用吗? – Ahamed 2012-02-28 18:38:39
我不知道他们为什么返回为double的确切原因(可能是因为当谈论维度时,你谈论的是精度,而double更多地用于这些情况),但对于setters来说,它只是一些方法重载帮助用户。 – talnicolas 2012-02-28 18:41:30
感谢您的回复,我不认为具有int值的double将有助于提高精度。我的意思是双1517.00等于1517,所以肯定有一些原因? – Ahamed 2012-02-28 18:44:54