113javaSystem类对IO的支持

81 阅读1分钟

System类对IO的支持

 

print(),println(),实际上在之前使用的系统输出就利用了IO流的模式完成在System类中实际上定义有三个操作常量

 

标准输出:

(显示器)public static final PrintStream out

错误输出:

public static final PrintStream err

标准输入(键盘)public static final InputSteam in

 

系统输出:

系统输出一个有两个常量:

out ,err 而且这两个常量所表示的都是PrintStream类的对象。(Out 用户能看到的err用户看不到的)

 

范例:

Try{

       Integer.parseInt(“abc”);

} catch(Exception e){

System.err.println(e);

System.out.println(e);

}

 

Sysstem.err也只是作为一个保留属性提供存在的,唯一可能使用的System.out(不会在开发中出现)。

从另外一个方面来讲。由于System.out属于Printstream类的实例化对象,而PrintStream又属于OutputStream类的子类,所以直接用Systemm.out为OutputStream实例化,OutputStream输出的位置将变为屏幕

 

范例:

所以System.out为OutputStream实例化

Outputstream out =System.Out;  //子类实例向上转型

Out ,write(“世界,和平”.getBytes());

 

输出:世界,和平

 

总结:   

抽象类不同的子类针对于同一个方法有不同的实现,而用户调用的核心(OutPutStream)