无法解释程序结果~

fpwcs 2008-04-23 04:09:51
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啊!



...全文
78 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
likai22 2008-04-23
  • 打赏
  • 举报
回复
太长了
&永无止境 2008-04-23
  • 打赏
  • 举报
回复
可以参见http://topic.csdn.net/u/20080423/13/4707b290-a5ea-4cfe-b5fa-b9b9c585c3c1.html如果没有重写toString()方法就调用Object类的toString()方法,@C17164就是调用Object类的toString()方法的结果
joejoe1991 2008-04-23
  • 打赏
  • 举报
回复
Object类的toString()方法,默认返回的是:类名+@+十六进制的哈希码
也就是:
getClass().getName()+"@"+Integer.toHexString(this.hashCode());

我们应该重写这个方法,让它返回一个更具有意义的字符串。
anqini 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fpwcs 的回复:]
那请问出的结果又为什么是@C17164这个呢>?
[/Quote]

toString()如下!

getClass().getName() + '@' + Integer.toHexString(hashCode())

@C17164,前面是不是还输出了你的类名字??清楚了吧!
KKK2007 2008-04-23
  • 打赏
  • 举报
回复

为什么执行PointTest后出来的结果是@C17164啊!
point是个对象,你直接这样output += "\n\nThe new location of point is " + point;
实际上输出的@C17164是 point对象的引用地址。
要想输出point的坐标,如楼上说,你要重写toString,或者写个别的方法输出点坐标
fpwcs 2008-04-23
  • 打赏
  • 举报
回复
那请问出的结果又为什么是@C17164这个呢>?
anqini 2008-04-23
  • 打赏
  • 举报
回复
output += "\n\nThe new location of point is " + point;
因为一个对象遇到字符串相加的时候自动调用其toString方法!但是你没有重写toString!自己重写把!
joejoe1991 2008-04-23
  • 打赏
  • 举报
回复
重写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
song_ice 2008-04-23
  • 打赏
  • 举报
回复
output += "\n\nThe new location of point is " + point;
输出时自动调用point.toString()

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧