简体   繁体   English

为什么我们不能在PrintStream类的帮助下调用'println()'方法,其中out是这个类的对象?

[英]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 of PrintStream class where out 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.

相关问题 为什么我不能在java.io.PrintStream中使用print()或println()方法,因为它是在导入类之后? - Why can't I use the print() or println() method in java.io.PrintStream as it is after importing the class? 为什么我们需要 System class 来调用 out.println() 方法? - Why do we need System class to call out.println() method? 为什么我们不能在方法之外使用类的对象来调用类的方法? - Why can't we call the method of the class using the class's object outside a method? PrintStream对象输出是通过null初始化的,我们如何在其上调用方法? - PrintStream object out is initialized by null, how we call method on it? 为什么`System.out.println(null);`give“方法println(char [])对于类型PrintStream错误是不明确的”? - Why does `System.out.println(null);` give “The method println(char[]) is ambiguous for the type PrintStream error”? 为什么PrintStream.java中的println(Object x)方法从同步块外部调用String.valueOf()? - Why does the println (Object x) method in PrintStream.java call String.valueOf() from outside the synchronized block? 我们可以为字符串调用Object类的toString()方法吗 - can we call the toString() method of the Object class for a String 为什么System.out.println()中不需要java.io.PrintStream? - why java.io.PrintStream is not needed in System.out.println()? 'println()'是PrintStream类或实例成员函数的静态成员函数吗? - Is 'println()' a static member function of PrintStream class or instance member function? 为什么我们不能在扩展类的静态方法中使用此实例? - Why can't we use this instance in the static method of extended class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM