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啊!

解决方案 »

  1.   

    output += "\n\nThe new location of point is " + point; 
    输出时自动调用point.toString()
      

  2.   

    重写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 
      

  3.   

    output += "\n\nThe new location of point is " + point; 
    因为一个对象遇到字符串相加的时候自动调用其toString方法!但是你没有重写toString!自己重写把!
      

  4.   

    那请问出的结果又为什么是@C17164这个呢>?
      

  5.   


    为什么执行PointTest后出来的结果是@C17164啊! 
    point是个对象,你直接这样output += "\n\nThe new location of point is " + point;
    实际上输出的@C17164是 point对象的引用地址。
    要想输出point的坐标,如楼上说,你要重写toString,或者写个别的方法输出点坐标
      

  6.   


    toString()如下!getClass().getName() + '@' + Integer.toHexString(hashCode())@C17164,前面是不是还输出了你的类名字??清楚了吧!
      

  7.   

    Object类的toString()方法,默认返回的是:类名+@+十六进制的哈希码
    也就是:
    getClass().getName()+"@"+Integer.toHexString(this.hashCode());我们应该重写这个方法,让它返回一个更具有意义的字符串。
      

  8.   

    可以参见http://topic.csdn.net/u/20080423/13/4707b290-a5ea-4cfe-b5fa-b9b9c585c3c1.html如果没有重写toString()方法就调用Object类的toString()方法,@C17164就是调用Object类的toString()方法的结果