java 何时调用 toString() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27545324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is toString() method called
提问by Leo
I got an example to override the toString() method.
我有一个例子来覆盖 toString() 方法。
import java.util.Scanner;
public class Demo{
String name = "";
String age = "";
Demo(String name, String age){
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name:");
String name = scanner.next();
System.out.print("Enter the age:");
String age = scanner.next();
Demo test = new Demo(name, age);
System.out.println(test);
}
public String toString() {
return ("Name=>" + name + " and " + "Age=>" + age);
}
}
My doubt is, how is it printing test as Name=>abc and Age=>12 even when toString() method is not being called neither from constructor nor from main?
我的疑问是,即使没有从构造函数或 main 调用 toString() 方法,它如何将测试打印为 Name=>abc 和 Age=>12?
回答by Jon Skeet
You're calling System.out.println(test)
, which is a call to PrintStream.println(Object)
. That will call toString()
on the reference you pass in, via String.valueOf
:
您正在调用System.out.println(test)
,这是对 的调用PrintStream.println(Object)
。这将调用toString()
您传入的参考,通过String.valueOf
:
Prints an Object and then terminate the line. This method calls at first
String.valueOf(x)
to get the printed object's string value, then behaves as though it invokesprint(String)
and thenprintln()
.
打印一个对象,然后终止该行。此方法首先调用
String.valueOf(x)
以获取打印对象的字符串值,然后表现得好像它调用了print(String)
,然后println()
.
回答by Sam Redway
Your toString() method is actually being called by the println method. By the way when you override a method I suggest you use the @Override notation above it, that way the compiler will check that you are actually overriding an existing method and it also keeps it clear for yourself and others when you read back through it. e.g.
您的 toString() 方法实际上是由 println 方法调用的。顺便说一下,当你覆盖一个方法时,我建议你使用它上面的@Override 符号,这样编译器会检查你是否真的覆盖了一个现有的方法,并且在你回读它时它也会为你自己和其他人保持清晰。例如
@Override
public void methodToBeOverriden () {}
回答by Eugene
You have called System.out.println(test);and the effect of it is like you are calling
你已经调用了 System.out.println(test); 它的效果就像你在打电话
System.out.println(test.toString());
System.out.println(test.toString());
回答by James Fox
When you call System.out.println(test)
it calls the toString()
method, using the object you passed in. As you have overridden this method, it is calling your toString()
method hence the expected output - Name=>abc and Age=>12
当您调用System.out.println(test)
它时toString()
,使用您传入的对象调用该方法。当您覆盖此方法时,它正在调用您的toString()
方法,因此是预期的输出 -Name=>abc and Age=>12
回答by Sumit Singh
If you put object in System.out.println
which is a call to toString()
如果您将对象放入System.out.println
其中调用toString()
Demo test = new Demo(name, age);
System.out.println(test);
From println(java.lang.Object)
Prints an
Object
and then terminate the line. This method calls at firstString.valueOf(x)
to get the printed object's string value, then behaves as though it invokesprint(String)
and thenprintln()
.
打印一个
Object
然后终止该行。此方法首先调用String.valueOf(x)
以获取打印对象的字符串值,然后表现得好像它调用了print(String)
,然后println()
.