主动加载&被动加载

 

 

 

package com.fh.reflection;

public class T {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        zhudongLoad();
    }

    /**
     * 被动加载
     */
    public static void beidongLoad(){
        System.out.println(Son.b);
        Son[] sons = new Son[10];
        int m1 = Son.M;
    }

    /**
     * 主动加载
     */
    public static void zhudongLoad() {
        //1、new方式主动加载
//        Son son = new Son();

        //2、反射方式主动加载
//        Class<Son> sonClass = Son.class;
//        sonClass.newInstance();

        //3-1、调用类变量(除了final常量)和类方法
//        int m = Son.m;
//        System.out.println(m);
        //3-2、调用类变量(除了final常量)和类方法
//         Son.m1();
        //3-3、调用类变量(除了final常量)和类方法
        Son.M2();

        //3-4、调用类变量(除了final常量)和类方法
//        int m1 = Son.M;
    }
}

class Father {
    static int b = 2;

    static {
        System.out.println("父类被加载");
    }
}

class Son extends Father {
    static {
        System.out.println("子类被加载");
        m = 300;
    }

    static int m = 100;
    static final int M = 1;

    public static void m1() {

    }

    public static final void M2() {

    }
}

 

 

posted @ 2022-06-01 11:38  禁止摆烂  阅读(42)  评论(0)    收藏  举报