Java toString 方法(对象)

发布于 2024-09-26 21:35:40 字数 311 浏览 2 评论 0原文

class Position {

    private double x,y;
    private int id;

    public String toString(Position a){

        String words ="(" + a.x + "," +a.y + ")";
        return words;

所以我在这里返回了一个内存地址。我做错了什么?我想获取使用 setter 设置的 x 和 y 的实际值。我也有吸气剂,我尝试不把 ax 放在 getX() 上,但这仍然给了我另一个内存地址。我做错了什么?

class Position {

    private double x,y;
    private int id;

    public String toString(Position a){

        String words ="(" + a.x + "," +a.y + ")";
        return words;

So I'm getting a memory address returned here. What am I doing wrong? I want to get the actual values of x and y that were set using setters. I also have getters, and I tried instead of putting a.x putting getX(), but that still give me another memory address. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的奇迹 2024-10-03 21:35:40

尝试:

public String toString(){

您拥有的代码正在添加一个新方法,而不是覆盖 Object 现有的无参数 toString 方法。这意味着旧方法仍然是被调用的方法,并且它给出了您所看到的输出。

Try:

public String toString(){

The code you have is adding a new method, instead of overriding the existing parameterless toString method of Object. That means the old method is still the one being called, and it gives the output you're seeing.

傲娇萝莉攻 2024-10-03 21:35:40

您实际上并没有重写 toString ;相反,您通过定义一个具有相同名称但需要不同参数的方法来重载它。您没有将 Position 传递给 toString;它应该引用当前实例。

You're not actually overriding toString; rather, you're overloading it by defining a method with the same name but which expects different arguments. You don't pass a Position to toString; it should refer the current instance.

恏ㄋ傷疤忘ㄋ疼 2024-10-03 21:35:40

作为对其他帖子的补充,您为什么认为需要将 Position 的引用传递给 toString() 方法。毕竟,该方法存在于同一个类 Position 中。您可以直接使用变量/属性,而无需像这样的任何引用。

  public String toString(){
        return "(" + x + "," + y + ")";
  }

或者,如果您想专门有一个参考,那么您可以这样做,

  public String toString(){
        return "(" + this.x + "," + this.y + ")";
  }

我在重构后将该方法制作为一个衬里。

如果您有兴趣了解人们更喜欢哪个版本,请参阅 这里,什么时候使用this。这是关于Java 中重写如何工作的教程/说明

As a complement to other posts, why do you think you need to pass Position's reference to the method toString(), anyway. After all, the method exist in the same class, Position. You can use the variable/properties directly without any reference like this.

  public String toString(){
        return "(" + x + "," + y + ")";
  }

Or in case you like to specifically have a reference then you can do it like this,

  public String toString(){
        return "(" + this.x + "," + this.y + ")";
  }

I made the method one liner after refactoring.

In case you are interested in knowing which version folks like more, please refer to here, when to use this. And here is the tutorial/explanation on how overriding works in Java.

坏尐絯℡ 2024-10-03 21:35:40

由于这是一项作业,我会要求您单步调试调试器。即使您期望它这样做,您的方法也不会被调用。 ( toString() 和 toString(Someobject ) 是不同的。

Since it is a homework, I would ask you step through a debugger. Your method is not called even though you expect it do so. ( toString() and toString(Someobject ) are different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文