为什么我不能像导入类一样在 java.io.PrintStream 中使用 print() 或 println() 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33431944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:39:17  来源:igfitidea点击:

Why can't I use the print() or println() method in java.io.PrintStream as it is after importing the class?

java

提问by nohup

Apologies for this silly question, but while I was learning java classes, I tried the following

为这个愚蠢的问题道歉,但是在我学习 Java 类时,我尝试了以下方法

javap -c java.lang.System | grep -i out
  public static final java.io.PrintStream out;

javap java.io.PrintStream | grep print
public void print(boolean);
public void print(char);
public void print(int);
public void print(long);
public void print(float);
public void print(double);
public void print(char[]);
public void print(java.lang.String);
public void print(java.lang.Object);
public void println();
public void println(boolean);
public void println(char);
public void println(int);
public void println(long);
public void println(float);
public void println(double);
public void println(char[]);
public void println(java.lang.String);
public void println(java.lang.Object);
public java.io.PrintStream printf(java.lang.String, java.lang.Object...);
public java.io.PrintStream printf(java.util.Locale, java.lang.String, java.lang.Object...);

And I tried to see if I can import java.io.PrintStreamand use print()or println()as it is, instead of System.out.println().

我试图看看我是否可以导入java.io.PrintStream和使用print()println()按原样使用,而不是System.out.println().

import java.io.PrintStream;
println('a');

And it came out with a compile error saying

它出现了一个编译错误说

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method print(char) is undefined for the type array
    at array.main(array.java:16)

Why can't I use println()as it is after importing java.io.Printstream?

为什么println()导入后无法使用java.io.Printstream

回答by Grodriguez

Because printlnis an instance method of the PrintStreamclass, and you need an instance of a class to call instance methods.

因为println是类的实例方法PrintStream,而你需要一个类的实例来调用实例方法。

However, System.outis an instance of PrintStream, so you can do:

但是,System.out是 的一个实例PrintStream,因此您可以执行以下操作:

 System.out.println("blah blah")

or you can create a new PrintStreaminstance, for example to write to a file:

或者您可以创建一个新PrintStream实例,例如写入文件:

 PrintStream p = new PrintStream(filename);
 p.println("blah blah");

This section in the Java Tutorial can be helpful: Lesson: Classes and Objects

Java 教程中的这一部分可能会有所帮助:课程:类和对象

回答by David Kühner

You need an instance of PrintStream because println is not static.

您需要一个 PrintStream 实例,因为 println 不是静态的。

You can try this:

你可以试试这个:

import java.io.PrintStream;
PrintStream printStream = new PrintStream(System.out);
// or better
PrintStream printStream = System.out;
printStream.println('a');

PrintStream needs a OutputStream for the constructor, you can give the OutputStream you want:

PrintStream 的构造函数需要一个 OutputStream,你可以给你想要的 OutputStream:

ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream

ByteArrayOutputStream、FileOutputStream、FilterOutputStream、ObjectOutputStream、OutputStream、PipedOutputStream

Javadoc : OutputStreamPrintStream

的Javadoc: OutputStream中的PrintStream

回答by Arun Raaj

You will have to instantiate PrintStream class but it doesn't have a default no-arg constructor.
So it's an easy way to use its static instance from System class and call the print() method directly.

您必须实例化 PrintStream 类,但它没有默认的无参数构造函数。
因此,使用 System 类中的静态实例并直接调用 print() 方法是一种简单的方法。

回答by dany-freezee

In Java You always need to call a method (function) on a specific object. That's why if you want to call any of these methods (print, println) you need to create the object of type java.io.PrintStream first.

在 Java 中,您总是需要对特定对象调用方法(函数)。这就是为什么如果要调用这些方法(print、println)中的任何一个,您需要先创建 java.io.PrintStream 类型的对象。

For example, try the following code:

例如,尝试以下代码:

import java.io.PrintStream;
...
PrintStream ps = System.out;
ps.print('a');

It creates the PrintStream object which prints to the cosole and prints the given char argument there.

它创建 PrintStream 对象,该对象打印到 cosole 并在那里打印给定的 char 参数。