標籤:
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("WangWang"); 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); }}
java類基礎