JAVA學習筆記(九)- 初始化塊與靜態代碼塊,java學習筆記

來源:互聯網
上載者:User

JAVA學習筆記(九)- 初始化塊與靜態代碼塊,java學習筆記
初始化塊

/* * 初始化塊 * 初始設定變數方式:聲明時、建構函式、代碼塊 * 使用static關鍵字修飾的代碼塊,稱為靜態代碼塊 *  * 執行順序:靜態代碼塊>代碼塊>構造方法 *  * 靜態代碼塊在類載入時執行,且只會執行一次,主要用於初始化靜態變數 * 代碼塊和構造方法在建立對象時執行 */public class Test01{    public static void main(String[] args)    {        Car car = new Car(60000);// 建立一個對象        car.show();// 調用對象的方法        System.out.println("-------華麗的分隔線-------\n");        Car car2=new Car(80000);        car2.show();    }}/* * 汽車類 */class Car{    // 屬性    String brand = "奇瑞";// 品牌,執行個體變數    double price;// 價格    String color;// 顏色    static int seat;// 座位元,靜態變數    public Car()    {        System.out.println("無參的構造方法");    }    // 帶參的構造方法,用來初始化成員    public Car(double price)    {        System.out.println("通過構造方法初始設定變數price");        this.price = price;    }    // 代碼塊    {        System.out.println("通過代碼塊初始設定變數color");        color = "red";    }    // 靜態代碼塊    static    {        System.out.println("通過靜態代碼塊初始設定變數seat");        seat=5;    }    public void show()    {        System.out.println("品牌:" + brand);        System.out.println("價格:" + price);        System.out.println("顏色:" + color);        System.out.println("座位元:"+seat);    }}
靜態變數與代碼塊
/* * static * 靜態變數(類變數) * 靜態變數為類的所有對象所共有,即為類所有 *  * 靜態成員訪問方式:對名名.靜態變數  或  類名.靜態變數 *  * 結論: * 1.static可以修飾變數、方法、代碼塊、匿名類 * 2.靜態成員在類載入時初始化 * 3.靜態方法中只能訪問靜態成員,不能訪問非靜態成員 * 4.普通成員方法中可以訪問靜態成員 *  *  * 變數類型 * 從範圍上劃分:局部變數、成員變數 * 從變數所屬上劃分:執行個體變數、靜態變數 */public class Test02{    public static void main(String[] args)    {        Student stu1 = new Student("趙信");        stu1.age = 20;        stu1.hobby = "學習Java編程";        System.out.println("趙信的愛好:" + stu1.hobby);// 通過對象名訪問靜態變數        stu1.show();        stu1.hobby="愛好打魔獸";        Student stu2 = new Student("蓋倫", 35);        //stu2.hobby = "學習Java編程";        System.out.println("蓋倫的愛好:" + Student.hobby);// 通過類名訪問靜態變數        stu2.show();        Student stu3 = new Student("寒冰", 18, "學習Java編程");        stu3.show();        stu3.print();// 通過對象訪問靜態方法        Student.print();// 通過類名訪問靜態方法    }}/* * 學生類 */class Student{    String name;// 執行個體變數    int age;// 執行個體變數    static String hobby; // 靜態變數    // 無參的構造方法    public Student()    {    }    // 帶一個參數的構造方法    public Student(String name)    {        this.name = name;    }    // 帶兩個參數的構造方法    public Student(String name, int age)    {        this.name = name;        this.age = age;    }    // 帶三個參數的構造方法    public Student(String name, int age, String hobby)    {        super();        this.name = name;        this.age = age;        this.hobby = hobby;    }    // 輸出學生資訊,普通的成員方法    public void show()    {        System.out.println("姓名:" + name + ",年齡:" + age + ",愛好:" + hobby + "\n");        /*         * 可以在普通成員方法中訪問靜態成員         */        print();        System.out.println(hobby);    }    // 靜態方法    public static void print()    {        System.out.println("俺是靜態方法。。。。。");        //System.out.println("靜態方法中輸出-姓名:"+name);//靜態方法中不能訪問非靜態成員        System.out.println("靜態方法中輸出-愛好:"+hobby);//靜態方法中只能訪問靜態成員    }}
類中成員
/* * 類中成員 *  * this表示當前對象 * 1.訪問類中的成員 * 2.解決局部變數和成員變數名稱衝突 * 3.調用類中重載的構造方法 */public class Test03{    public static void main(String[] args)    {        Dog dog = new Dog("旺財", 2, "雌");        dog.show();    }}/* * 狗狗類 */class Dog{    // 靜態變數    static String hobby;    // 成員變數    String name;    int age;    String sex;    // 自訂無參的構造方法    public Dog()    {        super();// 調用父類無參的構造方法        System.out.println("無參的構造方法");    }    public Dog(String name)    {        this();// 調用類中重載的構造方法        System.out.println("帶有一個參數的構造方法");        this.name = name;    }    // 重載的構造方法,帶有兩個參數的構造方法    public Dog(String name, int age)    {        this(name);// 調用類中重載的構造方法        this.age = age;        System.out.println("帶有兩個參數的構造方法");    }    // 重載的構造方法,帶參的構造方法,用於初始化成員資訊    public Dog(String name, int age, String sex)    {        this(name, age);// 調用類中重載的構造方法        this.sex = sex;        System.out.println("帶有三個參數的構造方法");    }    // 成員方法    public void show()    {        System.out.println("名稱:" + name + ",狗齡:" + age + "性別:" + sex);    }    // 靜態方法    public static void play()    {        System.out.println("你正在接飛盤。。。。");    }    // 普通代碼塊    {        System.out.println("普通代碼塊");        sex = "雄";    }    // 靜態代碼塊(靜態初始化塊)    static    {        System.out.println("靜態代碼塊");        hobby = "接飛盤 ";    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.