概述:
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]