类和接口的加载理解

概述:

1、当一个类在初始化时,要求其父类全部都已经初始化过了
2、接口的加载和类的加载有些不同,接口初始化过程有且仅有的一种:在一个接口初始化时,并不要求父接口完全完成了初始化,只有在真正使用到父接口的时候才会初始化(如引用接口中定义的常量)。

代码

public class MyChildClass implements MyParentInterface {
    public static String name = "hello";
    static {
        System.out.println("class MyChildClass init");
    }
}
public class MyChildClass2 extends MyParentClass {
    public static String name = "hello2";
    static {
        System.out.println("class MyChildClass2 init");
    }
}
public class MyParentClass {
    public static Thread thread = new Thread(){
        {
            System.out.println("class MyParentClass init");
        }
    };
}
public interface MyParentInterface {
    public static final Thread thread = new Thread(){
        {
            System.out.println("interface MyParentInterface init");
        }
    };
}
public class MyTest {
    public static void main(String[] args) {
        System.out.println("======================子类实现父接口=======================");
        System.out.println(MyChildClass.name);
        System.out.println("使用了接口后...");
        System.out.println(MyParentInterface.thread);

        System.out.println("======================子类继承父类=======================");

        System.out.println(MyChildClass2.name);
        System.out.println("使用了接口后...");
        System.out.println(MyParentInterface.thread);

    }
}
======================子类实现父接口=======================
class MyChildClass init
hello
使用了接口后...
interface MyParentInterface init
Thread[Thread-0,5,main]
======================子类继承父类=======================
class MyParentClass init
class MyChildClass2 init
hello2
使用了接口后...
Thread[Thread-0,5,main]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值