zoukankan      html  css  js  c++  java
  • 尚硅谷面试第一季-03类初始化和实例初始化

      话不多说,先上代码:

      1 package 类初始化和实例初始化;
      2 
      3 /**
      4  * @author zsh
      5  * @company wlgzs
      6  * @create 2019-03-26 16:25
      7  * @Describe
      8  * 父类的类初始化<clinit>()方法:
      9  * (1)j = method()
     10  * (2)父类的静态代码块
     11  *
     12  * 父类的实例初始化方法:
     13  * (1)super()(最前)
     14  * (2)i = test()
     15  * (3)父类的非静态代码块
     16  * (4)父类的无参构造器(最后)
     17  *
     18  * 非静态方法前面其实有一个默认的对象this
     19  * this在构造器(或<init>()方法)它表示的是正在创建的对象,因为这里是在创建Son对象,所以
     20  * test()执行的是子类重写的代码(面向对象多态)
     21  *
     22  * 这里i=test()执行的是子类重写的test()方法
     23  */
     24 public class Father {
     25     private int i = test();
     26     private static int j = method();
     27 
     28     static {
     29         System.out.print("(1)");
     30     }
     31     Father(){
     32         System.out.print("(2)");
     33     }
     34     {
     35         System.out.print("(3)");
     36     }
     37 
     38     public int test() {
     39         System.out.print("(4)");
     40         return 1;
     41     }
     42 
     43     public static int method() {
     44         System.out.print("(5)");
     45         return 0;
     46     }
     47 }
     48 
     49 package 类初始化和实例初始化;
     50 
     51 /**
     52  * @author zsh
     53  * @company wlgzs
     54  * @create 2019-03-26 16:28
     55  * @Describe
     56  * 子类的类初始化<clinit>()方法:
     57  * (1)j = method()
     58  * (2)子类的静态代码块
     59  *
     60  * 先初始化父类:(5)(1)
     61  * 再初始化子类:(10)(6)
     62  *
     63  * 子类的实例初始化方法:
     64  * (1)super()(最前)(9)(3)(2)
     65  * (2)i = test() (9)
     66  * (3)子类的非静态代码块 (8)
     67  * (4)子类的无参构造器(最后) (7)
     68  *
     69  * 因为创建了两个Son对象,因此实例化方法<init>()执行两次
     70  * (9)(3)(2)(9)(8)(7)
     71  */
     72 public class Son extends Father {
     73     private int i = test();
     74     private static int j = method();
     75 
     76     static {
     77         System.out.print("(6)");
     78     }
     79     Son(){
     80         //子类构造器一定会调用父类的构造器
     81         //super();
     82         System.out.print("(7)");
     83     }
     84     {
     85         System.out.print("(8)");
     86     }
     87 
     88     public int test() {
     89         System.out.print("(9)");
     90         return 1;
     91     }
     92 
     93     public static int method() {
     94         System.out.print("(10)");
     95         return 0;
     96     }
     97 
     98     public static void main(String[] args) {
     99         Son s1 = new Son();
    100         System.out.println();
    101         Son s2 = new Son();
    102     }
    103 }

    运行结果:

    结果分析及重点讲解:

  • 相关阅读:
    springboot小技巧(转)
    spring boot项目如何测试,如何部署
    thymeleaf模板的使用(转)
    springboot+多数据源配置
    springboot+shiro
    springboot+jpa+thymeleaf增删改查的示例(转)
    SpringBoot ( 七 ) :springboot + mybatis 多数据源最简解决方案
    tcpdump查看某个端口数据
    oracle完全删除表空间
    检测python进程是否存活
  • 原文地址:https://www.cnblogs.com/zsh-blogs/p/10601392.html
Copyright © 2011-2022 走看看