为什么我们不能println()在PrintStream类的帮助下调用方法,其中out是这个类的对象?
import java.io.*;
class Demo {
public static void main(String[] args) {
PrintStream.out.println("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不能在这个类的对象的类的
println()帮助下调用方法:PrintStreamoutRun Code Online (Sandbox Code Playgroud)PrintStream.out.println("Hello");
三个原因:
a)它不是静态的 - 你需要一个PrintStream类的实例
b)它具有protected可见性 - 所以它不是accessibe.
c)out变量实际上是OutputStream- 所以它没有println方法.
要使用PrintStream,您需要执行以下操作:
final PrintStream ps = new PrintStream(new FileOutputStream(new File(filename)));
ps.println("Now is the time for all good men to come to the aid of their party.");
ps.close();
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请咨询Javadoc.
| 归档时间: |
|
| 查看次数: |
2088 次 |
| 最近记录: |