• 构造器初始化(static)


     1 package demo;
     2 
     3 /*
     4  * 在类 的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,
     5  * 它们仍旧会在任何方法(包括构造器)被调用之前得到初始化。
     6  */
     7 public class Test {
     8     public static void main(String[] args) {
     9         House h = new House();
    10         h.f();
    11     }
    12 }
    13 
    14 class Window {
    15     Window(int maker) {
    16         System.out.println("Window(" + maker + ")");
    17     }
    18 }
    19 
    20 class House {
    21     Window w1 = new Window(1);
    22 
    23     House() {
    24         System.out.println("House()");
    25         w3 = new Window(33);
    26     }
    27 
    28     Window w2 = new Window(2);
    29 
    30     void f() {
    31         System.out.println("f()");
    32     }
    33 
    34     Window w3 = new Window(3);
    35 }
    36 //结果:
    37 //Window(1)
    38 //Window(2)
    39 //Window(3)
    40 //House()
    41 //Window(33)
    42 //f()
     1 package demo;
     2 
     3 /*
     4  * 先执行static修饰的成员,而且只被执行一次
     5  */
     6 public class Test1 {
     7     public static void main(String[] args) {
     8         System.out.println("Creating new Cup() in main");
     9         new Cup();
    10         System.out.println("Creating new Cup() in main");
    11         new Cup();
    12         table.f2(1);
    13         cup.f3(1);
    14     }
    15 
    16     static Table table = new Table();
    17     static Cup cup = new Cup();
    18 }
    19 
    20 class Bowl {
    21     Bowl(int maker) {
    22         System.out.println("Bowl(" + maker + ")");
    23     }
    24 
    25     void f1(int maker) {
    26         System.out.println("f1(" + maker + ")");
    27     }
    28 }
    29 
    30 class Table {
    31     static Bowl bowl1 = new Bowl(1);
    32 
    33     Table() {
    34         System.out.println("Table()");
    35         bowl1.f1(1);
    36     }
    37 
    38     void f2(int maker) {
    39         System.out.println("f2(" + maker + ")");
    40     }
    41 
    42     static Bowl bowl2 = new Bowl(2);
    43 }
    44 
    45 class Cup {
    46     Bowl bowl3 = new Bowl(3);
    47     static Bowl bowl4 = new Bowl(4);
    48 
    49     Cup() {
    50         System.out.println("Cup()");
    51         bowl4.f1(2);
    52     }
    53 
    54     void f3(int maker) {
    55         System.out.println("f3(" + maker + ")");
    56     }
    57 
    58     static Bowl bowl5 = new Bowl(5);
    59 }
    60 结果:
    61 Bowl(1)
    62 Bowl(2)
    63 Table()
    64 f1(1)
    65 Bowl(4)
    66 Bowl(5)
    67 Bowl(3)
    68 Cup()
    69 f1(2)
    70 Creating new Cup() in main
    71 Bowl(3)
    72 Cup()
    73 f1(2)
    74 Creating new Cup() in main
    75 Bowl(3)
    76 Cup()
    77 f1(2)
    78 f2(1)
    79 f3(1)
  • 相关阅读:
    HDU
    HDU
    HDU
    【JZOJ4231】寻找神格【分块】
    【JZOJ4231】寻找神格【分块】
    【JZOJ4230】淬炼神体【数论,数学】【二分】
    【JZOJ4230】淬炼神体【数论,数学】【二分】
    【JZOJ4229】学习神技【数论,数学】
    【JZOJ4229】学习神技【数论,数学】
    【JZOJ3771】【NOI2015模拟8.15】小 Z 的烦恼【高精度】【数论】
  • 原文地址:https://www.cnblogs.com/whx20100101/p/7543985.html
Copyright © 2020-2023  润新知