คล้ายๆ eval() ใน Javascript น้ะจ้ะ
ตัวอย่างน้ะจ้ะ เดิมเขียนแบบนี้
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy"); System.out.println(sdf.format(new Date()));
แต่ถ้าจะเขียนแบบ Reflection จะได้แบบนี้
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy"); Method m = sdf.getClass().getMethod("format", Date.class); System.out.println(m.invoke(sdf, new Date()));
ข้างล่าง เป็น ดอกส์ (doc) โดยย่นย่อ ก็อปมาจาก yegor256
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..); method.invoke(obj, arg1, arg2,...);
เพิ่งรู้ว่า จาวาก็ทำอะไรพรรค์นี้ได้ เจ๋งดีเหมือนกันนะ
view detail