简体   繁体   English

toString(int variable,n)与toString()

[英]toString(int variable, n) vs toString()

I have been looking for the answer this question everywhere and I don't know what it is called, but what is the point of a toString method with a variable in its parenthesis? 我一直在到处寻找这个问题的答案,但我不知道它叫什么,但是在括号中带有变量的toString方法的意义是什么?

public static String toString(Student[] list, int n)
   {

   }

I cannot find this answer for some reason. 由于某种原因,我找不到此答案。 I didn't know you can provide input to a toString, as I thought that you didn't call the method directly, you would just print a string whenever you declare the class it is in. 我不知道您可以向toString提供输入,因为我认为您没有直接调用该方法,因此只要声明该类所在的类,就只打印一个字符串。

You can provide any parameters to any method, but toString is special in that it's used in a lot of the inner workings of Java. 您可以为任何方法提供任何参数,但是toString是特殊的,因为它在Java的许多内部工作中都使用过。

That said...this is not that toString method. 就是说...这不是那个toString方法。 The one you wish to override takes no parameters whatsoever. 您要覆盖的参数不带任何参数。

@Override
public String toString() {
    // implementation
}

A toString that takes parameters implies that the object doesn't have enough information to print about itself, which is contradictory, so this method would like be best renamed to something more appropriate. 带有参数的toString表示该对象没有足够的信息来显示有关其自身的信息,这是矛盾的,因此最好将此方法重命名为更合适的名称。

toString() can be over ridden in your specific class, 在您的特定类中可以重写toString(),

something like 就像是

public class ToStringEg {

private Student[] list;

private int n;

public ToStringEg(Student[] list, int n) {
    super();
    this.list = list;
    this.n = n;
}

@Override
public String toString() {
    StringBuffer buf = new StringBuffer();
    // You can access list and v varible required by you
    //finally create a buf based on your logic
    return buf.toString();
}

} }

toString() is just a method like anything else. toString()就像其他方法一样。 It just so happens that the toString() method with no arguments gets called automatically by a lot of things, like when trying to print out an object. 碰巧的是,很多事情都会自动调用不带参数的toString()方法,例如尝试打印对象时。 However, that does not prevent you from making another method named toString(int variable) . 但是,这并不妨碍您制作另一个名为toString(int variable) That method will never be called automatically, but you can still declare it and call it yourself if you want to do custom string manipulation in whatever program you are writing. 该方法永远不会被自动调用,但是如果您想在编写的任何程序中进行自定义字符串操作,您仍然可以声明并自行调用。

Your toString() method has totally different from the special method @Override public String toString() inherited from Object class. 您的toString()方法与从Object类继承的特殊方法@Override public String toString()完全不同。

I think you should change your method name to avoid confusion in future. 我认为您应该更改方法名称,以免将来造成混淆。

How toString() method automatically called during System.out.println() or String.valueOf(obj) ? 如何在System.out.println()String.valueOf(obj)期间自动调用toString()方法? NO MAGIC! 没有魔术! Just see the implementation of valueOf() method in String class. 只需看一下String类中valueOf()方法的实现即可。

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

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

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