类的主动使用2

2021-09-26  本文已影响0人  Shaw_Young
/**
 * 3.当使用类、接口的静态字段时(final修饰特殊考虑),比如,使用getstatic或者putstatic指令。(对应访问变量、赋值变量操作)
 */
public class ActiveUse2 {

    @Test
    public void test1() {
        System.out.println(User.num2);
    }

    @Test
    public void test2() {
//        System.out.println(CompareA.NUM1);
        System.out.println(CompareA.NUM2);
    }
}

class User {
    static {
        System.out.println("User类的初始化过程");
    }

    public static int num = 1;
    public static final int num1 = 1;
    public static final int num2 = new Random().nextInt(10);
}

interface CompareA {

    public static final Thread t = new Thread() {
        {
            System.out.println("CompareA的初始化");
        }
    };

    public static final int NUM1 = 1;
    public static final int NUM2 = new Random().nextInt(10);
}

梦想很模糊,去追,它会渐变清晰。青春励志,奋斗下去别放弃。

上一篇 下一篇

猜你喜欢

热点阅读