系統類——System類

System類的幾大功能:

  • 訪問系統屬性
  • 訪問環境變量
  • 標準輸入和輸出
  • 加載文件和動態鏈接庫
  • 複製數組
  • 系統退出命令
  • 執行垃圾回收

常用屬性方法:
這裏寫圖片描述

import java.util.Properties;
class Person1 {
    private String name;
    private int age;
    public Person1(String name,int age) {
        this.name  = name;
        this.age = age;
    }
    public String toString() {
        return ("姓名:"+name+",年齡"+age);
    }
    public void finalize() throws Throwable {
        System.out.println("釋放對象"+this);
        super.finalize();
    }
}
/**
 * System類
 * @author lzq
 *
 */
public class TestDemo2 {

    /**
     * 獲取系統屬性
     */
    public static void show() {
        Properties pr = System.getProperties(); //獲取系統屬性
        pr.list(System.out);                    //列出屬性
    }
    /**
     * 複製數組
     * 
     */
    public static void show1() {
        char[] src = {'A','B','C','D'};
        char[] dest = {'1','2','3','4'};
        System.arraycopy(src, 2, dest, 1, 2);
        for(char array:dest) {
            System.out.print(array+" ");
        }

    }
    /**
     * 計算程序執行時間
     */
    public static void show2() {
        long start = System.currentTimeMillis();
        System.out.println("開始時間:"+start);
        long rus = 0;
        for(long i = 1;i < 88888888L;i++) {
            rus = rus+i;
        }
        long end = System.currentTimeMillis();
        System.out.println("結束時間:"+end);
        System.out.println("運行時間:"+(end-start));
    }
    /**
     * 垃圾對象的回收
     */
    public static void show3() {
        Person1 p = new Person1("小強",21);
        System.out.println(p);
        p = null;
        System.gc();
    }
    public static void main(String[] args) {
        show();
        show1();
        show2();
        show3();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章