Overriding toString method

后端 未结 7 499

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:53

    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.

提交回复
热议问题