java程序执行顺序

一、java中的执行顺序

1.   父类静态块
2.   自身静态块
3.   父类块
4.   父类构造器
5.   自身块
6.   自身构造器

2. *DOG父类 
3. */  
4.public class Dog {  
5.    public Dog() {  
6.        System.out.println("Dog");  
7.    }  
8.    static{  
9.        System.out.println("super static block");  
10.    }  
11.      
12.    {  
13.        System.out.println("super block");  
14.    }  
15.}  

 

2. * 子类藏獒 
3. */  
4.public class Mastiff extends Dog {  
5.    public Mastiff() {  
6.        System.out.println("Mastiff");  
7.    }  
8.  
9.    {  
10.        System.out.println("block");  
11.          
12.    }  
13.    static {  
14.        System.out.println("static block");  
15.    }  
16.      
17.    public static void  main(String[] args){  
18.        Mastiff mastiff=new Mastiff();        
19.    }  
20.}  

 运行的结果为:
super static block
static block
super block
Dog
block
Mastiff

 

二、JAVA中赋值顺序

1.父类的静态变量赋值
2.自身的静态变量赋值
3.父类成员变量赋值
4.父类块赋值
5.父类构造器赋值
6.自身成员变量赋值
7.自身块赋值
8.自身构造器赋值

2. *DOG父类 
3. */  
4.public class Dog {  
5.    public String type="父类成员变量赋的值";  
6.    public Dog() {  
7.        System.out.println("父类构造器--type-->"+type);  
8.        type="父类构造器赋的值";  
9.                   System.out.println("父类构造器----type--->"+type);  
10.    }  
11.      
12.    {  
13.        System.out.println("block---type--->"+type);  
14.        type="父类块赋的值";  
15.    }  
16.}  

 

2. * 子类藏獒 
3. */  
4.public class Mastiff extends Dog {  
5.    public String type="成员变量赋的值";  
6.    public Mastiff() {  
7.        System.out.println("构造器---type--->"+type);  
8.        type="构造器赋的值";  
9.    }  
10.      
11.    public void say(){  
12.        System.out.println("say---type---->"+type);  
13.    }  
14.  
15.    {  
16.        System.out.println("block---type--->"+type);  
17.        type="块赋的值";  
18.          
19.    }  
20.      
21.    public static void  main(String[] args){  
22.        Mastiff mastiff=new Mastiff();  
23.        mastiff.say()</span><span style="font-size: medium;">;</span><span style="font-size: medium;">        
24.    }  
25.}  

 执行结果如下:       
block---type--->父类成员变量赋的值
父类构造器--type-->父类块赋的值
父类构造器----type--->父类构造器赋的值
block---type--->成员变量赋的值
构造器---type--->块赋的值
say---type---->构造器赋的值

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值