5

为什么你可以通过使用方法并引用值从 java.awt.Point 类中获取 x 和 y 值?

Point p = new Point(10,20);
int x0 = p.getX();
int y0 = p.getY();
int x1 = p.x;
int y1 = p.y;
System.out.println(x0+"=="+x1+"and"+y0+"=="+y1);

制作这个类的人是否忘记将 x 和 y 设为私有?

4

2 回答 2

5

查看javadoc,这些似乎返回不同的类型。 p.x返回一个intwhilep.getX()返回一个double

的源代码Point显示了这一点:

public int x;
//...
public double getX() {
    return x;
}

所以看起来这是它的唯一目的。 getX()是一种更方便的获取坐标的方法double

于 2013-06-13T20:36:15.840 回答
1

改成

 double x0 = p.getX();

 // getX returns the X coordinate of this Point2D in double precision
于 2013-06-13T20:37:07.157 回答