问题
How come you can get the x and y values from a java.awt.Point class by using a method and referencing the value?
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);
Did the people who made this class forget to make x and y private?
回答1:
Looking at the javadoc, these seem to return different types. p.x returns an int while p.getX() returns a double.
The source code of Point shows this:
public int x;
//...
public double getX() {
return x;
}
So it looks like that's its only purpose. getX() is a more convenient way to get the coordinates as a double.
回答2:
Change to
double x0 = p.getX();
// getX returns the X coordinate of this Point2D in double precision
来源:https://stackoverflow.com/questions/17096533/two-ways-to-get-value-of-point-object