学习编程思想
1 package com.test.java.classs; 2 3 /** 4 * Created by Administrator on 2015/12/7. 5 * 在类的内部,变量定义的顺序决定了初始化的顺序。 6 * 变量会在任何方法(包含构造器)被调用之前得到初始化 7 */ 8 public class Variable { 9 public static void main(String[] args) { 10 House house = new House(); 11 house.f(); 12 } 13 14 } 15 16 class Window{ 17 public Window(int n) { 18 System.out.println("window-"+n); 19 } 20 } 21 22 class House{ 23 //1 24 Window w1 = new Window(1); 25 26 public House() { 27 //4 28 System.out.println("house()"); 29 //5 30 w4 = new Window(44); 31 //6 32 Window w2 = new Window(2); 33 } 34 //2 35 Window w3 = new Window(3); 36 void f(){ 37 System.out.println("f()"); 38 } 39 //3 40 Window w4 = new Window(4); 41 42 }
结果:
window-1
window-3
window-4
house()
window-44
window-2
f()
时间: 2024-11-15 07:28:03