简体   繁体   English

重写toString方法

[英]Overriding toString method

I am using .toString to return a string representation of an object, ie 我正在使用.toString返回对象的字符串表示形式,即

jcb.engineMove(move.toString());

will produce e2e4. 将产生e2e4。

What I am trying to do is to extract the text of this object (e2e4) as a string. 我想做的是提取此对象(e2e4)的文本作为字符串。 After Googling I came across overriding the toString method so I came up with this: 在谷歌搜索之后,我遇到了重写toString方法的问题,所以我想到了:

@Override
public String toString() {
    String s = "";
    int newRank = getRank();
    int newFile = getFile();
    final Move move = new Move(rank, file, newRank, newFile);
    s+="" + move;
    return s;
}

My questions are fairly basic: 我的问题很基本:

  1. is this the right approach 这是正确的方法
  2. How do I call this routine when trying to get the text of the object? 尝试获取对象的文本时如何调用此例程?

Overriding Object.toString is a good approach. 覆盖Object.toString是一个很好的方法。

However , your current implementation makes a major mistake by creating a new Move object (see below). 但是 ,当前的实现通过创建一个新的Move对象而犯了一个重大错误(请参见下文)。

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. 如果toString()仅用于调试(如mre所述),则可以实现另一个名为getText方法,该方法可以执行相同的操作。

IMPORTANT NOTE: 重要的提示:

You should not be creating a new Move object inside its toString method. 不应在其toString方法内创建新的Move对象。

This is a very bad idea (as mentioned by others). 这是一个非常糟糕的主意(正如其他人所提到的)。

Your toString method should simply build a string and return it. 您的toString方法应该只构建一个字符串并返回它。

is that toString() implemented in the Move class? 是在Move类中实现的toString() 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. 而且...我不太明白为什么要创建一个Move类的新实例。

Anyway, to generate the string representation in the Move class try with something like this: 无论如何,要在Move类中生成字符串表示形式,请尝试如下所示:

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. 然后,如果要获取字符串表示形式,您实际上正在做什么( jcb.engineMove(move.toString()); )并不是一个坏方法。

Object#toString的使用应仅限于调试。

I hope this is not code of toString() method in Move class. 我希望这不是Move类中toString()方法的代码。 The reason of my fear is that you are creating Move object inside of it and invoke recursively toString() method by s+="" + move; 我担心的原因是要在其中创建Move对象,然后通过s+="" + move;递归调用toString()方法s+="" + move; (this is same as s+=move.toString() ). (与s+=move.toString() )。

  1. To overwrite the toString() method is the common and right way to implement a custom textual representation of an object. 覆盖toString()方法是实现对象的自定义文本表示形式的常见且正确的方法。 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. 在Java中(就像其他语言(如C#)一样, toString()方法是在对象类型中定义的,这意味着Java中的每个对象都具有此方法。 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. 如果您的自定义对象(继承自class object )覆盖了toString()方法,则您的基类将提供此方法的新实现,该实现会将父类的toString()方法隐藏/忽略。

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. 这意味着当您在自定义类A中定义自定义toString()方法时,对该类型的实例(例如a)的调用将导致a.toString()调用您的实现。

  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. 在这种情况下,我可能不会使用toString(),因为看起来您只是在重复Move类中的逻辑。 In order to add any other details, I have one question: to what class are you adding this toString() method? 为了添加任何其他详细信息,我有一个问题:您将此toString()方法添加到哪个类?

  2. You call this method just like any other method. 您可以像调用其他任何方法一样调用此方法。 First you need an instance of an object to call it with: 首先,您需要一个对象的实例来调用它:

    someObj.toString(); 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. 正如mre所说,您不应将toString()用于代码所依赖的功能。 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. 我认为您的engineMove方法应该使用Move对象,而不是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()内创建一个占用时间和资源的新Move对象? 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; toString()应该在该类的实例上进行操作,因此您不必使用s + =“” + move创建一个新的实例,甚至更多。 will implicitly call toString() on the new Move object, which will call it again on a new Move object... 会在新的Move对象上隐式调用toString(),后者会在新的Move对象上再次调用它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM