简体   繁体   English

Android中PrintWriterPrinter的用途是什么?

[英]What is the use of PrintWriterPrinter in Android?

During researches within Android's Developer Database, I stumbled upon PrintWriterPrinter (inside android.util): https://developer.android.com/reference/android/util/PrintWriterPrinter.html 在Android开发人员数据库中进行研究期间,我偶然发现了PrintWriterPrinter(位于android.util内部): https : //developer.android.com/reference/android/util/PrintWriterPrinter.html

The source code of the PrintWriterPrinter class is shown below: 下面显示了PrintWriterPrinter类的源代码:

package android.util;

import java.io.PrintWriter;

/**
 * Implementation of a {@link android.util.Printer} that sends its output
 * to a {@link java.io.PrintWriter}.
 */
public class PrintWriterPrinter implements Printer {
    private final PrintWriter mPW;

    /**
     * Create a new Printer that sends to a PrintWriter object.
     * 
     * @param pw The PrintWriter where you would like output to go.
     */
    public PrintWriterPrinter(PrintWriter pw) {
        mPW = pw;
    }

    public void println(String x) {
        mPW.println(x);
    }
}

As you can see, it only has one single method: println(String x) which essentially applies println to the passed PrintWriter. 如您所见,它只有一个方法:println(String x),它实际上将println应用于传递的PrintWriter。 My question: Why would I use this useless PrintWriterPrinter if I could just manually call println on my PrintWriter object instead of creating a PrintWriterPrinter and then call println on that (which then calls println on a PrintWriter)? 我的问题:为什么我会用这个没用PrintWriterPrinter如果我可以手动调用的println我PrintWriter对象上,而不是建立PrintWriterPrinter,并调用的println上(然后调用的println上一个PrintWriter)?

The key is implements Printer . 关键是implements Printer

When you learned Java programming, you learned about Java interfaces . 学习Java编程时,您了解了Java接口 A class declares that it implements an interface via the implements keyword; 一个类声明它通过implements关键字实现了一个接口。 PrintWriterPrinter implements Printer indicates that PrintWriterPrinter implements the Printer interface. PrintWriterPrinter implements Printer表示PrintWriterPrinter实现Printer接口。

If you look at the Printer interface in the JavaDocs, you will see that it defines that sole println() method. 如果您查看JavaDocs中的Printer接口,您将看到它定义了唯一的println()方法。 However, if you look carefully, you will see that the JavaDocs list four implementations of that interface: LogPrinter , PrintStreamPrinter , PrintWriterPrinter , and StringBuilderPrinter . 但是,如果仔细看,您会发现JavaDocs列出了该接口的四个实现: LogPrinterPrintStreamPrinterPrintWriterPrinterStringBuilderPrinter

Through what is known as polymorphism , other code can work with an object known to implement the Printer interface — calling println() on that object — without knowing or caring which of these four implementations lies behind that object. 通过所谓的多态性 ,其他代码可以与已知实现Printer接口的对象一起工作—在该对象上调用println() ,而无需知道或关心这四个实现中的哪个位于该对象后面。 Other developers could create yet other implementations of the Printer interface (eg, one that stores the last 50 lines in an ArrayList ), and the rest of the framework needing a Printer could use that custom implementation without issue. 其他开发人员可以创建Printer接口的其他实现(例如,将最后50行存储在ArrayList ),其余需要Printer的框架可以使用该自定义实现而不会出现问题。

是否可以在PrintWriter上隐藏某些特定用途的方法,例如冲洗/关闭方法?

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

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