标签:style blog color os ar 2014 div sp log
1 public class Car{ 2 3 String name; 4 int age; 5 6 public Car(String name, int age){ 7 System.out.println("执行构造器"); 8 this.name = name; 9 this.age = age; 10 } 11 12 static { 13 System.out.println("执行静态初始化块"); 14 } 15 16 { 17 System.out.println("执行非静态初始化块"); 18 wih = 95.5; 19 } 20 double wih; 21 22 public String toString(){ 23 System.out.println("toString方法被执行了"); 24 return "名字是[" + name +" 年纪是:" + age + " 体重是:" + wih + "]"; 25 } 26 } 27 28 public class Test{ 29 public static void main(String[] args){ 30 Car c1 = new Car("小X", 17); 31 System.out.println(c1); 32 Car c2 = new Car("小天", 18); 33 System.out.println(c2); 34 } 35 }
静态初始化块在程序第一次j加载的时候执行,只执行一次,后面都就不执行了...
标签:style blog color os ar 2014 div sp log
原文地址:http://www.cnblogs.com/279842439yyw/p/3958657.html