public class reflectDemo { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in);//-------------------------提醒用戶輸入 System.out.println("請輸入類名(全路徑:package.className)"); String cname=in.nextLine(); System.out.println("屬性名"); String fname=in.nextLine(); Class cla = Class.forName(cname);//----------------------------動態加載類 Field fil = cla.getDeclaredField(fname);//---------------------動態獲取屬性 Object obj = cla.newInstance();//------------------------------動態建立對象 Object val = fil.get(obj);//-----------------------------------獲取查詢屬性的值 System.out.println("查詢的屬性值爲:"+val);//--------------------控制檯打印 } }