[英]Method return type in Dimension class of java.awt
I am surprised to see that getters of height
and width
members has return
type double
, albeit they are int
. 我很惊讶地看到
height
和width
成员的getter return
类型为double
,尽管它们是int
。 Moreover, setSize
method with double parameters has the following definition: 此外,具有双参数的
setSize
方法具有以下定义:
/**
* 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);
}
Please have a look at Dimension class. 请看一下Dimension类。 Above comment says values cannot go beyond Integer.MAX_VALUE.
上面的评论说,值不能超过Integer.MAX_VALUE。 Why?
为什么? Why do we have
double
in between? 为什么我们之间有
double
? Is there any subtle reason? 有什么微妙的原因吗? Can anyone please explain this to me?
有人可以向我解释一下吗? Sorry for my insistence!
对不起我的坚持!
The class is storing height
and width
as int
, it just provides a method that accept double too so you can call it with double values (but they are immediately cast to int). 该类将
height
和width
存储为int
,它只提供一个接受double的方法,因此您可以使用double值调用它(但它们会立即转换为int)。 There are others setSize()
methods in this file that accept int
values or even a Dimension
object. 此文件中还有其他
setSize()
方法接受int
值或甚至是Dimension
对象。
And as these values are stored as int
, of course their maximum value is Integer.MAX_VALUE
. 由于这些值存储为
int
,当然它们的最大值是Integer.MAX_VALUE
。
java.awt.Dimension
was retrofitted to fit into the java.awt.geom
package, so that it can be used wherever a Dimension2D
is required. java.awt.Dimension
经过改造以适应java.awt.geom
包,因此可以在需要Dimension2D
任何地方使用它。 The interface for the later deals with floating point, so Dimension
has to also. 后面的接口处理浮点,所以
Dimension
也必须。 Being limited to int
fields, only a subset of double
s can be represented. 仅限于
int
字段,只能表示double
s的子集。 Dimension2D.Float
is similarly restricted. Dimension2D.Float
受到类似限制。
You can use the java Dimension class with ints. 您可以将java Dimension类与int一起使用。 If you'd need a Dimension class with double width and height, youn could use the following:
如果您需要具有双倍宽度和高度的Dimension类,则可以使用以下内容:
public class DoubleDimension {
double width, height;
public DoubleDimension(double width, double height) {
super();
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "DoubleDimension [width=" + width + ", height=" + height + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DoubleDimension other = (DoubleDimension) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
return false;
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.