62,623
社区成员
发帖
与我相关
我的任务
分享import javax.swing.JOptionPane;
class Point {
private int x; // x part of coordinate pair
private int y; // y part of coordinate pair
// no-argument constructor
public Point()
{
}
// constructor
public Point( int xValue, int yValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
}
// set x in coordinate pair
public void setX( int xValue )
{
x = xValue; // no need for validation
}
// return x from coordinate pair
public int getX()
{
return x;
}
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
// return y from coordinate pair
public int getY()
{
return y;
}
/**
这里重写了toString()方法
*/
public String toString() {
return this.x+","+this.y;
}
// return String representation of Point object
} // end class Point
// Fig. 9.5: PointTest.java
// Testing class Point.
public class Main {
public static void main( String[] args )
{
Point point = new Point( 72, 115 ); // create Point object
// get point coordinates
String output = "X coordinate is " + point.getX() +
"\nY coordinate is " + point.getY();
point.setX( 10 ); // set x-coordinate
point.setY( 20 ); // set y-coordinate
// get String representation of new point value
output += "\n\nThe new location of point is " + point;
JOptionPane.showMessageDialog( null, output ); // display output
System.exit( 0 );
} // end main
} // end class PointTest