java 中 System 類
最常見到 System.out.println();
System類 定義為 public final class System extends Object{} 包含幾個有用的類字段和方法,用了關鍵字 final 修飾,表示此類不能被其他類繼承。
其構造方法為 private System{} (構造方法私有化,不能被外部實例化)。
System 中有三個屬性:in,out,err;
1.private final static InputStream in=null;
2.private final static PrintStream out=null;
--> out 為類PrintStream 類型 println()就是PrintStream中的方法。out被 final 和static 修飾的常量,故可以用System調用out屬性,再調用println()方法。
3.private final static PrintStream err=null;
System 中的幾個方法:
System 中的方法全部用 static 修飾,可以用類名稱直接調用,例如 System.getProperties();
1,public static Properties getProperties(){} System.getProperties().list(System.out); -->確定當前系統屬性。
2,public static String getProperties(String key){} System.getProperty(String key); --> 獲取當前鍵指定的系統屬性。
3,public static long currentTimeMillis(){} System.currentTimeMillis(); --> 返回當前時間(以毫秒為單位)。
public class S{
public static void main(String[] args){ long startTime=System.currentTimeMillis(); int a=0; for(int i=0;i<1000000000;i++){ a+=i; } long endTime=System.currentTimeMillis(); System.out.println("執行此程序用了"+(endTime-startTime)+"毫秒。"); } }
------------> 輸出為: 執行此程序用了380毫秒。
4,public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length){} 將指定源數組中的數組從指定位置復制到目標數組的指定位置。
public class S{
public static void main(String[] args){ int[] a=new int[]{1,2,3,10,20,30,4,5,6}; int[] b=new int[3]; System.arraycopy(a,3,b,0,3); for(int i=0;i<b.length;i++){ System.out.print(b[i]+" "); } }
-------------> 輸出為:10 20 30 ------> a表示數組a, 3表示 a 數組坐標(10),b表示數組b,0 表示 b 數組的坐標 0,3表示拷貝的長度。