Two ways to get value of Point object?

六眼飞鱼酱① 提交于 2019-12-01 17:34:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!