标签:
1 public class MyJingTaiDaimaKuai 2 { 3 public static int i = test1();//1.最开始执行 4 public int age = test2();//new的时候先执行成员变量的初始化,再执行构造函数 5 6 static MyJingTaiDaimaKuai() 7 { 8 Console.WriteLine("静态代码块");//2.再执行 9 } 10 public MyJingTaiDaimaKuai()//4.//6. 11 { 12 Console.WriteLine("构造函数"); 13 } 14 15 public static int test1() 16 { 17 Console.WriteLine("test1"); 18 return 3; 19 } 20 21 public static int test2()//3.//5. 22 { 23 Console.WriteLine("test2"); 24 25 return 333; 26 } 27 } 28
执行代码
1 MyJingTaiDaimaKuai a1 = new MyJingTaiDaimaKuai(); 2 Console.WriteLine(a1.age); 3 4 MyJingTaiDaimaKuai a2 = new MyJingTaiDaimaKuai(); 5 Console.WriteLine(a2.age);
执行结果
标签:
原文地址:http://www.cnblogs.com/lucyliang/p/4749162.html