public 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;
} // return String representation of Point object
} // end class Point// Fig. 9.5: PointTest.java
// Testing class Point.
import javax.swing.JOptionPane;public class PointTest { 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为什么执行PointTest后出来的结果是@C17164啊!
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;
} // return String representation of Point object
} // end class Point// Fig. 9.5: PointTest.java
// Testing class Point.
import javax.swing.JOptionPane;public class PointTest { 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为什么执行PointTest后出来的结果是@C17164啊!
输出时自动调用point.toString()
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
因为一个对象遇到字符串相加的时候自动调用其toString方法!但是你没有重写toString!自己重写把!
为什么执行PointTest后出来的结果是@C17164啊!
point是个对象,你直接这样output += "\n\nThe new location of point is " + point;
实际上输出的@C17164是 point对象的引用地址。
要想输出point的坐标,如楼上说,你要重写toString,或者写个别的方法输出点坐标
toString()如下!getClass().getName() + '@' + Integer.toHexString(hashCode())@C17164,前面是不是还输出了你的类名字??清楚了吧!
也就是:
getClass().getName()+"@"+Integer.toHexString(this.hashCode());我们应该重写这个方法,让它返回一个更具有意义的字符串。