class Student() {public Student (){System.out.println("這是建構(gòu)方法")}}
class ConstructDemo{public static void main(String[] args){Student s =new Student();
System.out.println(s);
#}}? 為什麼「這是建構(gòu)方法」沒有在main方法中呼叫就能在控制臺輸出
因?yàn)閖ava程式一般入口是在main函數(shù),從mian函數(shù)開始執(zhí)行。主函數(shù)中實(shí)例化了一個(gè)學(xué)生對象,一個(gè)物件在實(shí)例化的時(shí)候會(huì)呼叫這個(gè)類別的建構(gòu)方法,在學(xué)生類別中定義了該類別的無參的建構(gòu)方法,方法裡有輸出語句。