zoukankan      html  css  js  c++  java
  • java静态数据初始化

    java中的静态数据初始化是比较复杂的,看下面的演示程序:
    class Bowl {

    Bowl(int marker) {
    System.out.println("Bowl(" + marker + ")");
    }

    void f(int marker) {
    System.out.println("f(" + marker + ")");
    }
    }

    class Table {
    static Bowl b1 = new Bowl(1);                        //1
    static Bowl b2 = new Bowl(2);                        //2
    Table() {
    System.out.println("Table()");                    //3
    b2.f(1);                                        //4
    }

    void f2(int marker) {
    System.out.println("f2(" + marker + ")");
    }


    }

    class Cupboard {
    Bowl b3 = new Bowl(3);                                //7            //11        //15
    static Bowl b4 = new Bowl(4);                        //5
    static Bowl b5 = new Bowl(5);                        //6

    Cupboard() {
    System.out.println("Cupboard()");                //8            //12        //16
    b4.f(2);                                        //9            //13        //17
    }

    void f3(int marker) {
    System.out.println("f3(" + marker + ")");
    }


    }

    public class StaticInitialization {
    static Table t2 = new Table();
    static Cupboard t3 = new Cupboard();
    public static void main(String[] args) {
    System.out.println("Creating new Cupboard() in main");    //10
    new Cupboard();                           
    System.out.println("Creating new Cupboard() in main");    //14
    new Cupboard();
    t2.f2(1);                                        //18
    t3.f3(1);                                        //19
    }


    }
    输出结果为:
    Bowl(1)
    Bowl(2)
    Table()
    f(1)
    Bowl(4)
    Bowl(5)
    Bowl(3)
    Cupboard()
    f(2)
    Creating new Cupboard() in main
    Bowl(3)
    Cupboard()
    f(2)
    Creating new Cupboard() in main
    Bowl(3)
    Cupboard()
    f(2)
    f2(1)
    f3(1)
    注意标注的程序运行顺序
  • 相关阅读:
    关于在centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module
    MSTP协议介绍和堆叠技术介绍
    RSTP技术详解
    5招解决路由黑洞
    系统批量运维管理器Fabric之部署LNMP业务环境
    系统批量运维管理器Fabric之动态获取远程目录列表
    系统批量运维管理器Fabric之查看远程主机信息
    系统批量运维管理器Fabric之基本语法篇
    系统批量运维管理器Fabric之环境搭建篇
    LightGBM 调参方法(具体操作)
  • 原文地址:https://www.cnblogs.com/macula7/p/1960583.html
Copyright © 2011-2022 走看看