• 对象初始化优先级


    初始化优先级:静态代码块>>main方法>>构造代码块>>构造函数

     1 package com.itinfo;
     2 
     3 /**
     4  * 代码块的优先级
     5  * http://www.cnblogs.com/goodcheap
     6  *
     7  * @author: Wáng Chéng Dá
     8  * @create: 2017-02-26 19:12
     9  */
    10 public class CodeBlock {
    11 
    12     static {
    13         System.out.println("go to------>>>>>(CodeBlock--static{})");
    14         System.out.println("I am CodeBlock static code block!
    ");
    15     }
    16 
    17     {
    18         System.out.println("go to------>>>>>(CodeBlock--{})");
    19         System.out.println("I am CodeBlock code block!
    ");
    20     }
    21 
    22     public CodeBlock() {
    23         System.out.println("go to------>>>>>(CodeBlock--CodeBlock(){})");
    24         System.out.println("I am CodeBlock Constructor!
    ");
    25     }
    26 
    27 }
    28 class TestCodeBlock extends CodeBlock {
    29 
    30     static {
    31         System.out.println("go to------>>>>>(TestCodeBlock--static{})");
    32         System.out.println("I am TestCodeBlock static code block!
    ");
    33     }
    34 
    35     {
    36         System.out.println("go to------>>>>>(TestCodeBlock--{})");
    37         System.out.println("I am TestCodeBlock code block!
    ");
    38     }
    39 
    40     public TestCodeBlock() {
    41         System.out.println("go to------>>>>>(TestCodeBlock--public TestCodeBlock(){})");
    42         System.out.println("I am TestCodeBlock Constructor!
    ");
    43     }
    44 
    45     public static void main(String[] args) {
    46         System.out.println("go to------>>>>>(main)");
    47         System.out.println("I am main method!
    ");
    48         CodeBlock block = new TestCodeBlock();
    49     }
    50 }

    控制台输出:

    go to------>>>>>(CodeBlock--static{})
    I am CodeBlock static code block!

    go to------>>>>>(TestCodeBlock--static{})
    I am TestCodeBlock static code block!

    go to------>>>>>(main)
    I am main method!

    go to------>>>>>(CodeBlock--{})
    I am CodeBlock code block!

    go to------>>>>>(CodeBlock--CodeBlock(){})
    I am CodeBlock Constructor!

    go to------>>>>>(TestCodeBlock--{})
    I am TestCodeBlock code block!

    go to------>>>>>(TestCodeBlock--public TestCodeBlock(){})
    I am TestCodeBlock Constructor!

     

    多态状态对象初始化优先级总结:父类静态代码块>>子类静态代码块>>main方法>>父类构造代码块>>父类构造函数>>子类构造代码块>>子类构造函数

  • 相关阅读:
    poj3617Best Cow Line
    Apache Thrift的简单使用
    UIControl-IOS开发
    怎样学习嵌入式
    JAVA学习第六十二课 — TCP协议练习
    腾讯2014年实习生招聘笔试面试经历
    一分钟明确 VS manifest 原理
    国外破解站点大全
    Android 4.4 KitKat NotificationManagerService使用具体解释与原理分析(一)__使用具体解释
    linux中fork()函数具体解释(原创!!实例解说)
  • 原文地址:https://www.cnblogs.com/chinda/p/6445552.html
Copyright © 2020-2023  润新知