zoukankan      html  css  js  c++  java
  • 3.06_面向对象(代码块的概述和分类)

    * A:代码块概述
      * 在Java中,使用{}括起来的代码被称为代码块。
    * B:代码块分类
      * 根据其位置和声明的不同,可以分为局部代码块,构造代码块,静态代码块,同步代码块(多线程)。
    * C:常见代码块的应用
      * a:局部代码块
      * 在方法中出现;限定变量生命周期,及早释放,提高内存利用率
      * b:构造代码块 (初始化块)
      * 在类中方法外出现;多个构造方法方法中相同的代码存放到一起,每次调用构造都执行,并且在构造方法前执行
      * c:静态代码块
      * 在类中方法外出现,并加上static修饰;用于给类进行初始化,在加载的时候就执行,并且只执行一次。
      * 一般用于加载驱动

    * A:看程序写结果
    *
        class Student {
          static {
            System.out.println("Student 静态代码块");
          }

          {
            System.out.println("Student 构造代码块");
          }

            public Student() {
              System.out.println("Student 构造方法");
              }
              }

          class Demo2_Student {
            static {
              System.out.println("Demo2_Student静态代码块");
              }

              public static void main(String[] args) {
                System.out.println("我是main方法");

                  Student s1 = new Student();
                  Student s2 = new Student();
                  }
                }

  • 相关阅读:
    背水一战 Windows 10 (90)
    背水一战 Windows 10 (89)
    背水一战 Windows 10 (88)
    背水一战 Windows 10 (87)
    背水一战 Windows 10 (86)
    背水一战 Windows 10 (85)
    背水一战 Windows 10 (84)
    背水一战 Windows 10 (83)
    背水一战 Windows 10 (82)
    背水一战 Windows 10 (81)
  • 原文地址:https://www.cnblogs.com/zyyzy/p/12424612.html
Copyright © 2011-2022 走看看