Overriding toString method

后端 未结 7 502

I am using .toString to return a string representation of an object, i.e.

jcb.engineMove(move.toString());

will produce e2e4.

What

7条回答
  •  轮回少年
    2020-12-12 05:41

    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.

提交回复
热议问题