为什么我得到这个输出,但不使用字符串
why do I get this output, but don't use the String
public class Card {
private Rank rank;
private Suit suit;
public Card(Rank r, Suit s) {
this.suit = s;
this.rank = r;
}
@Override
public String toString() {
return rank + " of " + suit;
}
public static void main (String[] args) {
Card test = new Card(Rank.A, Suit.Clubs);
System.out.println(test);
}
}
所以在我的输出中我打印了 A of Clubs。但是我在任何地方都没有使用 toString() 我只是从我的构造函数中定义了一个新的 Card 。那么有人可以向我解释为什么我得到这个输出吗?
System.out.println(test);
toString()
将在 test
此处调用以打印输出。
如果您查看 PrintWriter.println(Object) 的 Javadoc 和源代码,您会看到
/**
* Prints an Object and then terminates the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>Object</code> to be printed.
*/
public void println(Object x) {
String s = String.valueOf(x);
synchronized (lock) {
print(s);
println();
}
}
反过来,String.valueOf(Object)
/**
* Returns the string representation of the {@code Object} argument.
*
* @param obj an {@code Object}.
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
所以你可以看到 toString()
是为你打电话的。
因为这在文档中,您可以假设这在将来或在其他实现中不会改变。即代码可能会更改,但记录的功能将始终保留。
当你打印出一个对象时,就像你在最后一行中所做的那样,它使用 toString() 方法中的任何内容,如果有的话。
public class Card {
private Rank rank;
private Suit suit;
public Card(Rank r, Suit s) {
this.suit = s;
this.rank = r;
}
@Override
public String toString() {
return rank + " of " + suit;
}
public static void main (String[] args) {
Card test = new Card(Rank.A, Suit.Clubs);
System.out.println(test);
}
}
所以在我的输出中我打印了 A of Clubs。但是我在任何地方都没有使用 toString() 我只是从我的构造函数中定义了一个新的 Card 。那么有人可以向我解释为什么我得到这个输出吗?
System.out.println(test);
toString()
将在 test
此处调用以打印输出。
如果您查看 PrintWriter.println(Object) 的 Javadoc 和源代码,您会看到
/**
* Prints an Object and then terminates the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>Object</code> to be printed.
*/
public void println(Object x) {
String s = String.valueOf(x);
synchronized (lock) {
print(s);
println();
}
}
反过来,String.valueOf(Object)
/**
* Returns the string representation of the {@code Object} argument.
*
* @param obj an {@code Object}.
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
所以你可以看到 toString()
是为你打电话的。
因为这在文档中,您可以假设这在将来或在其他实现中不会改变。即代码可能会更改,但记录的功能将始终保留。
当你打印出一个对象时,就像你在最后一行中所做的那样,它使用 toString() 方法中的任何内容,如果有的话。