Overriding toString method

和自甴很熟 提交于 2019-11-28 14:26:11

Overriding Object.toString is a good approach.

However, your current implementation makes a major mistake by creating a new Move object (see below).

To call the routine (once you fix it), do exactly what you're already doing:

jcb.engineMove(move.toString());

If toString() should only be used for debugging (as mre says) you could implement another method named getText that does the same thing.

IMPORTANT NOTE:

You should not be creating a new Move object inside its toString method.

This is a very bad idea (as mentioned by others).

Your toString method should simply build a string and return it.

is that toString() implemented in the Move class? if yes then what I see is an infinite loop. And... I don't really understand why you are creating a new instance of a Move class.

Anyway, to generate the string representation in the Move class try with something like this:

public class Move {

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(rank).append(file);
    builder.append(newRank).append(newFile);
    return builder.toString();
  }

}

Then if you want to get the string representation what you are actually doing (jcb.engineMove(move.toString());) isn't a bad approach.

the use of Object#toString should be limited to debugging.

I hope this is not code of toString() method in Move class. The reason of my fear is that you are creating Move object inside of it and invoke recursively toString() method by s+="" + move; (this is same as s+=move.toString()).

  1. To overwrite the toString() method is the common and right way to implement a custom textual representation of an object. You will find this procedure throughout literature and documentation.

  2. In Java (like it is the case in other languages like C#) the toString() method is defined in the object type which means that every object in Java has this method. If your custom object (which inherits from class object) overwrites the toString() method then your base class provides a new implementation of this method which hides/omits the toString() method from the super class.

That means when you define a custom toString() method in your custom class A a call to an instance of that type (say it's a) a.toString() would result in calling your implementation.

  1. I would probably not use toString() in this case because it appears that you are simply repeating the logic that is in the Move class. In order to add any other details, I have one question: to what class are you adding this toString() method?

  2. You call this method just like any other method. First you need an instance of an object to call it with:

    someObj.toString();

To give any more details, I need an answer to the previous question.

As mre said, you should not use toString() for something that your code depends on to function. Now, what are you trying to accomplish? Can you give any code from these classes? I would think your engineMove method should take a Move object, not a String. If you can give some more details, we might be able to steer you in a better direction.

Also, be careful with that code that you have. Why do you need to create a new Move object that takes up time and resources inside the toString()? toString() should operate on an instance of the class, so you shouldn't need to create a new one but even more so, using s+="" + move; will implicitly call toString() on the new Move object, which will call it again on a new Move object...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!