英文:
PrintWriter constructors
问题
我已经列出了 PrintWriter
类的 8 个构造函数。
PrintWriter(File file)
,PrintWriter(File file, String csn)
,PrintWriter(OutputStream out)
,
PrintWriter(OutputStream out, boolean autoFlush)
,PrintWriter(String fileName)
,
PrintWriter(String fileName, String csn)
,PrintWriter(Writer out)
,
PrintWriter(Writer out, boolean autoFlush)
。
问题:
- 如果没有接受 PrintStream 的 PrintWriter 构造函数,那么我们怎么能够写出像我下面写的语句呢?
- 如果没有接受 BufferedWriter 的 PrintWriter 构造函数,那么我们怎么能够写出像我下面写的语句呢?
PrintWriter writer1 = new PrintWriter(System.out);
PrintWriter writer2;
writer2 = new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, reportFileName))));
提前感谢您的回答。
英文:
I've listed below the 8 constructors of PrintWriter
class.
PrintWriter(File file)
, PrintWriter(File file, String csn)
, PrintWriter(OutputStream out)
,
PrintWriter(OutputStream out, boolean autoFlush)
, PrintWriter(String fileName)
,
PrintWriter(String fileName, String csn)
, PrintWriter(Writer out)
,
PrintWriter(Writer out, boolean autoFlush)
.
Questions:
-
If there's no PrintWriter contructor that takes PrintStream, then how come we can write a statment like I've written below?
-
If there's no PrintWriter contructor that takes BufferedWriter, then how come we can write a statment like I've written below?
PrintWriter writer1 = new PrintWriter(System.out); PrintWriter writer2; writer2 = new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, reportFileName))));
Thanks in advance.
答案1
得分: 2
这是可能的,因为PrintStream继承自FilterOutputStream,而后者再次继承自OutputStream。
一个BufferedWriter继承自Writer。
Writer和OutputStream是PrintWriter构造函数中可能的变量。我们在这里讨论的重要主题是继承。这有一个很好的帖子来解释这个。
英文:
This is possible because PrintStream inherits from FilterOutputStream and the latter again from OutputStream.
A BufferedWriter inherits from Writer.
Writer and OutputStream are possible variables in the PrintWriter constructors. The big topic, what we are talking about here, is inheritance. There is a good post for this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论