[英]Why we can't call 'println()' method with help of PrintStream class where out is object of this class?
Why we can't call println()
method with help of PrintStream class where out is object of this class? 为什么我们不能在PrintStream类的帮助下调用
println()
方法,其中out是这个类的对象?
import java.io.*;
class Demo {
public static void main(String[] args) {
PrintStream.out.println("Hello");
}
}
Why we can't call
println()
method with help ofPrintStream
class whereout
is object of this class:为什么我们不能在
PrintStream
类的帮助下调用println()
方法,其中out
是此类的对象:PrintStream.out.println("Hello");
Three reasons: 三个原因:
a) it's not static - you need an instance of the PrintStream class a)它不是静态的 - 你需要一个PrintStream类的实例
b) it's got protected
visibility - so its not accessibe. b)它具有
protected
可见性 - 因此它不是accessibe。
c) the out
variable is actually an OutputStream
- so it doesn't have a println
method. c)
out
变量实际上是一个OutputStream
- 因此它没有println
方法。
To use a PrintStream, you want to do something like this: 要使用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();
Consult the Javadoc for more information. 有关更多信息,请咨询Javadoc 。
Yep what Greg says. 是的,格雷格说。 Also if you want to print to the console you can just use
System.out.println("Manga Bunga");
此外,如果您想要打印到控制台,您可以使用
System.out.println("Manga Bunga");
And if you want to use PrintStream use the println() method after instantiating a PrintStreat object. 如果要使用PrintStream,请在实例化PrintStreat对象后使用println()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.