简体   繁体   English

java中的输出流

[英]Output Stream in java

Please explain out variable in System.out 请在System.out中解释变量

out is sometimes referred to be object of type OutputStream sometimes referred to as object of type PrintStream out有时被称为OutputStream类型的对象,有时也称为PrintStream类型的对象

and even when it is predefined variable, it is sometimes assigned it to PrintWriter object 即使它是预定义变量,有时也会将其分配给PrintWriter对象

PrintWriter out= response.getWriter();

is it due to the fact that a superclass reference can be assigned the reference to objects of its subclass? 是否因为超类引用可以被赋予对其子类的对象的引用?

Super class reference variable can hold the reference of sub-class object. 超类引用变量可以保存子类对象的引用。 The OutputStream is an abstract super class of all OutputStream of bytes classes, so you can say the System.out field as OutputStream type. OutputStream是所有OutputStream的字节类的抽象超类,因此您可以将System.out字段称为OutputStream类型。

PrintStream is a subclass of OutputStream, and System.out is a PrintStream, so it truly is both. PrintStream是OutputStream的子类, System.out是一个PrintStream,所以它确实是两者。

The line: 这条线:

PrintWriter out= response.getWriter();

has nothing to do with System.out. 与System.out无关。 I don't know where that line of code is from. 我不知道那行代码来自哪里。 It's defining a local variable named out that's totally independent of System. 它定义了一个名为out的局部变量out它完全独立于System。

To the best of my knowledge, in System.out, "out" is a name of a method. 据我所知,在System.out中,“out”是方法的名称。 When you say System.out.print() you call the System class and its out() method. 当您说System.out.print()时,您调用System类及其out()方法。 This out() is a static method which gives you the reference to "System" class object. 这个out()是一个静态方法,它为您提供对“System”类对象的引用。 So after you get the reference you call the print() method. 因此,在获得引用后,您将调用print()方法。

But in, PrintWriter out= response.getWriter(); 但是,PrintWriter out = response.getWriter(); you are just creating a reference variable of PrintWriter class. 您只是创建PrintWriter类的引用变量。 So as my fellow colleagues mention, there is no connection between "System.out" and "PrintWriter out". 正如我的同事们提到的那样,“System.out”和“PrintWriter out”之间没有联系。 There are two purposes for those two. 这两个目的有两个目的。

Correct me if I'm wrong. 如果我错了纠正我。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM