简体   繁体   English

println隐式调用Java方法......为什么?

[英]Java method implicitly called by println …why?

I have a question about why the toString() method is implicitly called when the main method calls the printPersonPrinting() method. 我有一个问题,为什么在main方法调用printPersonPrinting()方法时隐式调用toString()方法。

public class PersonPrinting {

    private String name;
    private int age;

    public PersonPrinting(String aName, int anAge) {
        name = aName;
        age = anAge;
    }

    public String toString() {
        return name + " " + age;
    }

    public void printPersonPrinting() {
        System.out.println(this);
    }

    public static void main (String [] args) {
        PersonPrinting p = new PersonPrinting("Dan",10);
        //printPersonPrinting is called, but toString isn't. 
       //But the output is formatted by toString method.
        p.printPersonPrinting();
    }
}

The output of this code is --> Dan 10 此代码的输出是 - > Dan 10

Thanks!! 谢谢!!

It's just the way Java works. 这就是Java的工作方式。 By default, System.out.println() calls the toString() method on its param object to convert it to its string representation. 默认情况下, System.out.println()调用其param对象上的toString()方法将其转换为其字符串表示形式。

If the object's class is not overriding the toString() method, you may get as a result something like className@someHexadecimalNumber . 如果对象的类没有覆盖toString()方法,则可能会得到类似className@someHexadecimalNumber的结果。 (which is the result of the default implementation of the toString() method in the Object class) (这是Object类中toString()方法的默认实现的结果)

Because System.out.println(this); 因为System.out.println(this); invokes 所调用

String.valueOf(this)

which in turn calls 反过来打电话

this.toString()

It's basically System.out.println(this.toString()); 它基本上是System.out.println(this.toString()); .

System.out is a PrintStream so you can see PrintStream.println(Object) which says (in part) System.out是一个PrintStream所以你可以看到PrintStream.println(Object) ,它说(部分)

This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println() . 此方法首先调用String.valueOf(x)来获取打印对象的字符串值,然后表现为调用print(String)然后调用println()

Java method implicitly called ... why? 隐式调用Java方法......为什么?

One answer is (basically) that that is the way that println(Object) is specified to work. 一个答案是(基本上),这是指定println(Object)工作的方式。

Another answer is that that is the best (simple) design alternative. 另一个答案是,这是最好的(简单)设计选择。 Consider: 考虑:

  • println(Object) is supposed to output a text rendering of the object println(Object)应该输出对象的文本呈现

  • there are two alternatives: 有两种选择:

    • use a generic mechanism to render the object 使用通用机制来渲染对象

    • use a rendering method (like toString() ) 使用渲染方法(如toString()

  • considering the "generic mechanism", how would it decide what fields of the object to render, and how to render them? 考虑到“通用机制”,它将如何决定要渲染的对象的哪些字段以及如何渲染它们? Unless the programmer can somehow tailor rendering, it is impossible for a generic mechanism to "get it right". 除非程序员能够以某种方式定制渲染,否则通用机制不可能“正确”。 And the tailoring, is liable to be more complicated than writing a toString() method. 剪裁可能比编写toString()方法更复杂

  • considering the toString() approach, the method can be overridden to render the object as the programmer wants ... or not. 考虑到toString()方法,可以重写该方法,以便像程序员想要的那样呈现对象。

Now admittedly, a simple toString() method is often not sufficiently flexible, but that can be addressed in a variety of ways; 现在可以肯定的是,简单的toString()方法通常不够灵活,但可以通过各种方式解决; eg 例如

  • use PrintWriter.format(...) to do the formatting / writing, or 使用PrintWriter.format(...)进行格式化/写入,或

  • use a formatter that generates a String and PrintWriter.print(String) . 使用生成String和PrintWriter.print(String)的格式化程序。

(For situations where you need to render and output large amounts of text, you need to use a different approach; ie not a PrintWriter . That is a different topic.) (对于需要渲染和输出大量文本的情况,您需要使用不同的方法;即不是PrintWriter 。这是一个不同的主题。)

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

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