簡體   English   中英

為什么我們不能在PrintStream類的幫助下調用'println()'方法,其中out是這個類的對象?

[英]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");
    }
}

為什么我們不能在PrintStream類的幫助下調用println()方法,其中out是此類的對象:

  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();

有關更多信息,請咨詢Javadoc

是的,格雷格說。 此外,如果您想要打印到控制台,您可以使用System.out.println("Manga Bunga");

如果要使用PrintStream,請在實例化PrintStreat對象后使用println()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM