【问题标题】:Java seems to be ignoring my toString methodJava 似乎忽略了我的 toString 方法
【发布时间】:2014-05-07 04:00:45
【问题描述】:

好的,所以我为这个坐标类创建了一个 toString() 方法,但是当我尝试使用 system.out.print() 打印坐标时,它似乎忽略了我的方法,只使用了 Object.toString( ) 方法,并且只返回一个内存地址。

这是我的 toString 方法的代码:

package spacetable;

public class Coordinate {
private int x;
private int y;

public Coordinate(){
    x=0;
    y=0;
}
public Coordinate(int x, int y){
    this.x = x;
    this.y = y;
}

public int getX(){
    return x;
}
public int getY(){
    return y;
}

public double distTo(Coordinate xy){
    double run = xy.getX() - this.getX();
    double rise = xy.getY() - this.getX();
    double dist = sqrt(run*run + rise*rise);
    return dist;
}
public double distTo(int x, int y){
    double run = x - this.getX();
    double rise = y - this.getX();
    double dist = sqrt(run*run + rise*rise);
    return dist;
}

@Override
public String toString(){
    String Strx = Integer.toString(x);
    String Stry = Integer.toString(y);
    String result = "(" Strx + ", " + Stry + ")";
    return result;  
}

}

以及我尝试打印的代码:

package spacetable;
public class CordinateTest {

public static void main(String[] args) {
    Coordinate place = new Coordinate(2,3);
    System.out.println(place);
}
}

输出是: spacetable.Coordinate@e53108

为什么我的 toString() 被忽略了?

【问题讨论】:

  • 你能给出一个简短但完整的例子来说明这个问题吗?您已经几乎做到了,但还没有到我们可以轻松重现问题的阶段。
  • 你不应该做 place.toString() 吗?
  • 您的代码(修复了语法错误并猜测了 Coordinate 构造函数)对我来说很好。您需要显示更多代码让我们弄清楚发生了什么。
  • @Sujan 不,toString() 在这种情况下会被自动调用。
  • 你确定这是你的编译代码吗?

标签: java overriding tostring


【解决方案1】:

你的代码对我来说很好,看看here

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{ 
    public static class Coordinate {
       private int x = 3;
       private int y = 5;

       @Override
       public String toString(){
          String Strx = Integer.toString(x);
          String Stry = Integer.toString(y);
          String result = "(" + Strx + ", " + Stry + ")";
          return result;
       }
   }

    public static void main (String[] args) throws java.lang.Exception
    {
        Coordinate place = new Coordinate();
        System.out.println(place);
    }
}

【讨论】:

  • 好吧,这很奇怪。当我在 Ideone 上运行时它运行正常,但它仍然无法在我的机器上运行......
  • 检查方法声明 toString() 如果您将大写字母与小写字母混合,则将调用对象类的原始对象。
  • 我在不同的包中重新创建了整个类,现在它正在工作...... eclipse 一定有一些奇怪的事情发生。
【解决方案2】:

您确定在添加 toString 方法后重新编译了吗?如果您使用的是 IDE,请检查是否设置了自动构建。如果不重新编译代码

另外,你能粘贴导入命令吗?只是想确保您正在导入自己的 Coordinate 类,而不是某个第三方 jar 的类

【讨论】:

  • 我正在使用 IDE(特别是 Eclipse)。我没有导入 Coordinate 类,它与我使用它的程序在同一个包中。
  • 另外,我确实检查以确保“自动生成”已打开
  • 你能在 ide 上尝试一个 clean 并再次运行。另外,在 print 语句上放一个断点,看看它调用的是哪个 toString 方法
  • 我不确定你所说的尝试干净是什么意思......但我设置了一个断点,据我所知,它正在调用 Object toString 方法。 “坐标(对象).toString()行:237 [局部变量不可用]”就是它所说的
【解决方案3】:
public class Coordinate {
    private int x;
    private int y;
    public Coordinate(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    public String toString()
    {

        String result = "("+ x + ", " + y + ")";
        return result;

    }
    public static void main(String[] args) {
         Coordinate place = new Coordinate(2,3);
            System.out.println(place);

    }

}

【讨论】:

  • 是的,你修正了他的错字。这段代码也适用于我。这个答案有何建设性?
猜你喜欢
  • 1970-01-01
  • 2016-11-11
  • 2021-10-28
  • 2016-09-30
  • 2016-03-16
  • 2014-02-03
  • 2012-03-11
  • 2013-09-07
  • 2016-08-12
相关资源
最近更新 更多