zoukankan      html  css  js  c++  java
  • Java学习之区块

    在创建一个对象时,会调用构造方法外还会执行其他区块的语句,例如静态块、构造块。

    执行顺序——静态块 -- main() -- 构造块 -- 构造方法

    静态块在类加载时执行一次,之后便不再执行。可以在对类进行初始化,也可以实例化对象调用方法。但不能使用this、super等关键词,因为此时并没有实例化的对象(除非你实例化一个对象,在调用方法,但还是不能使用this、super)。

    当有多个静态块时,按照先后顺序执行。

    当有子类继承时的执行循序(这里这篇文章写的挺好的,就没再自己写了)

    1. 执行父类的静态代码块,并初始化父类静态成员变量
    2. 执行子类的静态代码块,并初始化子类静态成员变量
    3. 执行父类的构造代码块,执行父类的构造函数,并初始化父类普通成员变量
    4. 执行子类的构造代码块, 执行子类的构造函数,并初始化子类普通成员变量

    父类静态 -- 子类静态 -- 父类构造代码块 -- 父类构造函数 -- 子类构造代码块 -- 子类构造函数

    我常忘记子类静态执行的循序!!!

    --------------20.9.22更新-----------------

    如果直接在声明类成员变量时对变量进行了初始化,会先进行初始化再调用构造函数。

    class X {
        Y b = new Y();
    
        X() {
            System.out.print("X");
        }
    }
    
    class Y {
        Y() {
            System.out.print("Y");
        }
    }
    
    public class Z extends X {
        Y y = new Y();
    
        Z() {
            System.out.print("Z");
        }
    
        public static void main(String[] args) {
            new Z();
        }
    }

    结果:YXYZ

    ----------------------------------------------

    对于父类中的的静态内部类只要不实例化内部类并不会执行内部类中的各个区块(即便是实例化了父类的对象也不会执行,除非父类的区块中有内部类的实例化语句)。

    父类

     1 public class BlockFather {
     2     String name;
     3 
     4     static class $BlockTest {
     5         static {
     6             System.out.println("父类的静态内部类静态块");
     7         }
     8         {
     9             System.out.println("父类的静态内部类构造块");
    10         }
    11         public $BlockTest() {
    12             System.out.println("父类的静态内部类构造方法");
    13         }
    14     }
    15 
    16     public BlockFather() {
    17         super();
    18         System.out.println("父类构造方法");
    19     }
    20 
    21     {
    22         System.out.print("父类构造块1		");
    23         this.name = "huahua";
    24     }
    25     {
    26         System.out.print("父类构造块2		");
    27         this.name = "huahua2";
    28     }
    29     static {
    30         System.out.print("父类静态块1		");
    31 //        System.out.println(new BlockFather().name = "静态块实例化对象");
    32     }
    33     static {
    34         System.out.print("父类静态块2		");
    35     }
    36 
    37     @Override
    38     public String toString() {
    39         return "BlockTest [name=" + name + "]";
    40     }
    41 }

    子类

     1 public class BlockSon extends BlockFather {
     2 
     3     static class $BlockTestSon extends BlockFather.$BlockTest {
     4         static {
     5             System.out.println("子类的静态内部类静态块");
     6         }
     7         {
     8             System.out.println("子类的静态内部类构造块");
     9         }
    10 
    11         public $BlockTestSon() {
    12             System.out.println("子类的静态内部类构造方法");
    13         }
    14     }
    15 
    16     int age;
    17 
    18     {
    19         System.out.print("子类构造块	");
    20     }
    21     static {
    22         System.out.println("子类静态块");
    23 //        {
    24 //            System.out.println("子类静态块内部");
    25 //        }
    26     }
    27 
    28     public BlockSon() {
    29         super();
    30         System.out.println("子类构造方法	");
    31     }
    32 
    33     public BlockSon(int age) {
    34         super();
    35         this.age = age;
    36     }
    37 }

    测试

     1 public class BlockTest {
     2     public static void main(String[] args) {
     3         System.out.println("----	new BlockFather");
     4         new BlockFather();
     5 
     6         System.out.println("----	new BlockSon");
     7         new BlockSon();
     8 
     9 //        System.out.println("----	new BlockFather.$BlockTest");
    10 //        new BlockFather.$BlockTest();
    11 
    12 //        System.out.println("----	BlockFather b = new BlockSon");
    13 //        BlockFather b = new BlockSon();
    14     }
    15 }

    参考 java构造块与静态块

    未经授权商用禁止,转载请标明出处,附上原文链接 个人能力有限,若有不足之处欢迎各位大佬指出
  • 相关阅读:
    Django基础命令
    ubuntu中python项目独立的虚拟环境
    Springboot项目的小问题
    redis
    ubuntu系统根目录下各个目录用途说明
    SpringBoot 在IDEA中实现热部署
    SpringBoot访问不到webapp下的内容
    httpServeltRequest和Model传值的区别
    map的输出
    主流框架排名
  • 原文地址:https://www.cnblogs.com/pong137/p/13681736.html
Copyright © 2011-2022 走看看