标签:
class Person { private String name; private int age; { //构造代码块:可以给【所有】对象进行初始化的 //放构造函数里的,共性初始化代码 //执行顺序:1.静态代码块,2.构造代码块,3.构造函数 System.out.println("Person run."); } static String country = "CN"; public void speak() { System.out.println(name+":"+age); } Person() { name = "baby"; age = 1; } Person(String n, int a) { age = a; name = n; } public void setName(String n) { name = n; } } class ConsDemo { public static void main (String[] args) { Person p = new Person(); p.speak(); Person p2 = new Person("旺财", 16); p2.setName("旺旺"); p2.speak(); System.out.println(Person.country); System.out.println(args); System.out.println(args.length); try { System.out.println(args[0]); } catch (Exception e) { System.out.println("log error:"+e); } StaticCode.show(); //静态类,类名进行方法调用 } } class StaticCode { static int num; static { //静态代码块,进行类的初始化 num = 10; System.out.println("Static init."); } static void show() { System.out.println(num); } }
标签:
原文地址:http://www.cnblogs.com/superkv/p/4324920.html