java运行时执行顺序

来源:互联网 发布:计算机的发展史软件 编辑:程序博客网 时间:2024/06/15 07:45
运行顺序为:
   父类静态块
   自身静态块
   父类块
   父类构造器
   自身块
   自身构造器 
    DOG类改进后源码 


Java代码  收藏代码
<span style="font-size: medium;">/** 
 *DOG父类 
 */  
public class Dog {  
    public Dog() {  
        System.out.println("Dog");  
    }  
    static{  
        System.out.println("super static block");  
    }  
      
    {  
        System.out.println("super block");  
    }  
}  
</span>  
  
   Mastiff改进后源码
Java代码  收藏代码
<span style="font-size: medium;">/** 
 * 子类藏獒 
 */  
public class Mastiff extends Dog {  
    public Mastiff() {  
        System.out.println("Mastiff");  
    }  
  
    {  
        System.out.println("block");  
          
    }  
    static {  
        System.out.println("static block");  
    }  
      
    public static void  main(String[] args){  
        Mastiff mastiff=new Mastiff();        
    }  
}  
</span>  
 
运行的结果为:
super static block
static block
super block
Dog
block
Mastiff
0 0
原创粉丝点击