ݺߣ

ݺߣShare a Scribd company logo
ʥ˼԰Ƽ޹˾
http://www.shengsiyuan.com
? JavaʱУһܷ֪࣬
  ЩԺͷһܷ
  һǿ϶ġֶ̬ȡϢԼ
  ̬öķĹJava Եķ䣨
  Reflectionơ
? Java Ҫṩ¹




                           2ҳ
?   ʱжһࡣ
?   ʱһĶ
?   ʱжһеijԱͷ
?   ʱһķ




                              3ҳ
? Reflection JavaΪ̬׼̬Եһ
  ؼʡʱ͸Reflection
  APIsȡκһ֪ƵclassڲϢ
  modifierspublic, static ȵȣsuperclass
  Objectʵ֮interfacesSerializable
  ҲfieldsmethodsϢʱ
  fieldsݻmethods




                                          4ҳ
? һԣȺ˵̬ԣͬһ
  ǣʱıṹͣ
  ԳΪ̬ԡ۵㿴PerlPython
  RubyǶ̬ԣC++JavaC#Ƕ̬




                             5ҳ
? ĶJavaǶ̬ԣ
  ȴһdzͻĶ̬ػƣ
  Reflectionֵ˼ǡ䡢ӳ󡢵
  ӰJavaָǿʱ
  ء֪̽ʹñڼȫδ֪classes
  仰˵JavaԼһʱŵ֪
  ƵclassϤ죨
  methods壩ʵ塢
  fieldsֵmethods֡͸
  classthe ability of the program to
  examine itselfΪintrospectionʡ
  ڹۡʡReflectionintrospectiondz
  

                                       6ҳ
? JDKУҪʵJavaƣЩ඼
  λjava.lang.reflect
 C   Classࣺһࡣ
 C   Field ࣺijԱԱҲΪԣ
 C   Methodࣺķ
 C   Constructor ࣺĹ췽
 C   Arrayࣺṩ˶̬飬ԼԪصľ̬




                                  7ҳ
? DumpMethodsʾReflection API
  ãȡвָȻ
  ӡеķϢ




                                8ҳ
? ReflectTester һʾReflection
  APIĻʹ÷ReflectTesterһ
  copy(Object object)ܹ
  һͲobject ͬ͵ĶȻ
  objectеԿ½ĶУ
  
? ֻܸƼ򵥵JavaBeanٶ
  JavaBean ÿԶpublic ͵
  getXXX()setXXX()




                                  9ҳ
? ReflectTester copy(Object object)
  ִ²
? 1öͣ
  C Class classType=object.getClass();
  C System.out.println("Class:"+classType.getNam
    e());




                                             10ҳ
? java.lang.Object жgetClass()
  ˶һJava󣬶ͨ
  ö͡ClassReflection API
  еĺ࣬·
  C   getName()֡
  C   getFields()public͵ԡ
  C   getDeclaredFields()ԡ
  C   getMethods()public͵ķ
  C   getDeclaredMethods()з




                                       11ҳ
? getMethod(String name, Class[]
  parameterTypes)ضname
  ָ֣parameterTypes ָ
  IJ͡

? getConstructors()public͵Ĺ
  췽
? getConstructor(Class[] parameterTypes)
  ض췽parameterTypes 
  ָ췽IJ͡
? newInstance()ͨIJĹ췽
  һ

                                      12ҳ
? 2ͨĬϹ췽һ¶
? Object
  objectCopy=classType.getConstructo
  r(new Class[]{}).newInstance(new
  Object[]{});
? ϴȵClass
  getConstructor()һ
  Constructor ĬϵĹ췽
  ȻConstructor
  newInstance()һʵ
                                  13ҳ
? 3öԣ
? Field
  fields[]=classType.getDeclaredFields(
  );
? Class getDeclaredFields()
  ԣpublicprotected
  ĬϺprivateʼ



                                    14ҳ
? 4ÿӦgetXXX()
  setXXX()ȻִЩԭ
  ԿµĶ




                       15ҳ
? InvokeTestermain()У
  ÷ƵһInvokeTester
  add()echo()




                           16ҳ
? add()Ϊint ͣñʾadd()
  MethodĴ£
? Method
  addMethod=classType.getMethod("add",new
  Class[]{int.class,int.class});
? Methodinvoke(Object obj,Object args[])
  յIJΪΪݣת
  ΪӦİװ͵Ķinvoke()ķֵ
  ʵʱõķķǻ
  ôinvoke()תΪӦİװ͵Ķ
  ٽ䷵




                                          17ҳ
? ڱУInvokeTester add()
  Լֵintͣadd Method 
  invoke()ʱֻܴInteger ͵IJ
  invoke()ķҲInteger ͣInteger
  int ͵İװࣺ

? Object result=addMethod.invoke(invokeTester,
? new Object[]{new Integer(100),new
  Integer(200)});
? System.out.println((Integer)result); //result Ϊ
  Integer




                                                18ҳ
? java.lang.Array ṩ˶̬ͷ
  Ԫصĸ־̬
? ArrayTester1 main()һ
  Ϊ10 ַ飬Űλ
  Ϊ5 ԪΪhelloȻٶȡ
  λΪ5 Ԫصֵ




                           19ҳ
? ArrayTester2 main()
  һ 5 x 10 x 15 飬
  λΪ[3][5][10] ԪصֵΪ37




                           20ҳ
? ֪JavaиObject class
  Java classesļ̳иԴ
  ӦJava classбд
  methodshashCode()equals()
  clone()toString()getClass()ȡ
  getClass()һClass object




                                21ҳ
? Class classʮ⡣һclassesһ̳
  ObjectʵԱJavaʱclasses
  interfacesҲenumarrayprimitive
  Java types
? boolean, byte, char, short, int, long, float,
  doubleԼؼvoidһclassأ򵱼
  class loaderdefineClass()JVMã
  JVM ԶһClass objectɡ
  Java׼Դ롱۲Class objectʵʱ
  Classconstructorһprintln()
  ܹΪClassûpublic constructor




                                             22ҳ
? ClassReflectionԴκ̽
  classΨΪһClass
  objectܾɺ߻Ϊʮ
  Reflection APIs




                          23ҳ
? JavaǴӶ;Ϊһclass
  ӦClass object




                       24ҳ
25ҳ
? ɶʵ壬Reflection ̬
  һԡԱctorһԡ
  ctorõǡctorͱ
  鷳ЩٵClassnewInstance()
  ǵConstructor newInstance()
  ׼һClass[]ΪctorIJָͣ
? ΪһdoubleһintȻԴΪԱ
  getConstructor()һרctor
  ׼һObject[] Ϊctorʵֵָ
  3.14159125רctor
  newInstance()


                                26ҳ
̬ɡClass object Ӧ֮classĶʵ壻
Ա




                                     27ҳ
28ҳ
? á֮ctor൱ơ׼
  һClass[]Ϊָͣһ
  StringһHashtableȻԴΪԱ
  getMethod()ضMethod object׼
  һObject[]ԱȻ֮ض
  Method objectinvoke()
? ΪʲôMethod objectʱָطͣ




                                   29ҳ
? Ϊmethod overloadingҪ
  signatureΨһطͲ
  signatureһɷݡ仰˵ֻҪָ
  methodƺͲУһָ
  һһ޶method




                        30ҳ
31ҳ
? ǰȣfieldݡ
  ɶˣΪҪԱ
  ȵClassgetField()ָfield
  ơضField objectֱ֮
  Fieldget()set()




                             32ҳ
33ҳ

More Related Content

[ʥ˼԰][Java SE]Reflection

  • 2. ? JavaʱУһܷ֪࣬ ЩԺͷһܷ һǿ϶ġֶ̬ȡϢԼ ̬öķĹJava Եķ䣨 Reflectionơ ? Java Ҫṩ¹ 2ҳ
  • 3. ? ʱжһࡣ ? ʱһĶ ? ʱжһеijԱͷ ? ʱһķ 3ҳ
  • 4. ? Reflection JavaΪ̬׼̬Եһ ؼʡʱ͸Reflection APIsȡκһ֪ƵclassڲϢ modifierspublic, static ȵȣsuperclass Objectʵ֮interfacesSerializable ҲfieldsmethodsϢʱ fieldsݻmethods 4ҳ
  • 5. ? һԣȺ˵̬ԣͬһ ǣʱıṹͣ ԳΪ̬ԡ۵㿴PerlPython RubyǶ̬ԣC++JavaC#Ƕ̬ 5ҳ
  • 6. ? ĶJavaǶ̬ԣ ȴһdzͻĶ̬ػƣ Reflectionֵ˼ǡ䡢ӳ󡢵 ӰJavaָǿʱ ء֪̽ʹñڼȫδ֪classes 仰˵JavaԼһʱŵ֪ ƵclassϤ죨 methods壩ʵ塢 fieldsֵmethods֡͸ classthe ability of the program to examine itselfΪintrospectionʡ ڹۡʡReflectionintrospectiondz 6ҳ
  • 7. ? JDKУҪʵJavaƣЩ඼ λjava.lang.reflect C Classࣺһࡣ C Field ࣺijԱԱҲΪԣ C Methodࣺķ C Constructor ࣺĹ췽 C Arrayࣺṩ˶̬飬ԼԪصľ̬ 7ҳ
  • 8. ? DumpMethodsʾReflection API ãȡвָȻ ӡеķϢ 8ҳ
  • 9. ? ReflectTester һʾReflection APIĻʹ÷ReflectTesterһ copy(Object object)ܹ һͲobject ͬ͵ĶȻ objectеԿ½ĶУ ? ֻܸƼ򵥵JavaBeanٶ JavaBean ÿԶpublic ͵ getXXX()setXXX() 9ҳ
  • 10. ? ReflectTester copy(Object object) ִ² ? 1öͣ C Class classType=object.getClass(); C System.out.println("Class:"+classType.getNam e()); 10ҳ
  • 11. ? java.lang.Object жgetClass() ˶һJava󣬶ͨ ö͡ClassReflection API еĺ࣬· C getName()֡ C getFields()public͵ԡ C getDeclaredFields()ԡ C getMethods()public͵ķ C getDeclaredMethods()з 11ҳ
  • 12. ? getMethod(String name, Class[] parameterTypes)ضname ָ֣parameterTypes ָ IJ͡ ? getConstructors()public͵Ĺ 췽 ? getConstructor(Class[] parameterTypes) ض췽parameterTypes ָ췽IJ͡ ? newInstance()ͨIJĹ췽 һ 12ҳ
  • 13. ? 2ͨĬϹ췽һ¶ ? Object objectCopy=classType.getConstructo r(new Class[]{}).newInstance(new Object[]{}); ? ϴȵClass getConstructor()һ Constructor ĬϵĹ췽 ȻConstructor newInstance()һʵ 13ҳ
  • 14. ? 3öԣ ? Field fields[]=classType.getDeclaredFields( ); ? Class getDeclaredFields() ԣpublicprotected ĬϺprivateʼ 14ҳ
  • 15. ? 4ÿӦgetXXX() setXXX()ȻִЩԭ ԿµĶ 15ҳ
  • 16. ? InvokeTestermain()У ÷ƵһInvokeTester add()echo() 16ҳ
  • 17. ? add()Ϊint ͣñʾadd() MethodĴ£ ? Method addMethod=classType.getMethod("add",new Class[]{int.class,int.class}); ? Methodinvoke(Object obj,Object args[]) յIJΪΪݣת ΪӦİװ͵Ķinvoke()ķֵ ʵʱõķķǻ ôinvoke()תΪӦİװ͵Ķ ٽ䷵ 17ҳ
  • 18. ? ڱУInvokeTester add() Լֵintͣadd Method invoke()ʱֻܴInteger ͵IJ invoke()ķҲInteger ͣInteger int ͵İװࣺ ? Object result=addMethod.invoke(invokeTester, ? new Object[]{new Integer(100),new Integer(200)}); ? System.out.println((Integer)result); //result Ϊ Integer 18ҳ
  • 19. ? java.lang.Array ṩ˶̬ͷ Ԫصĸ־̬ ? ArrayTester1 main()һ Ϊ10 ַ飬Űλ Ϊ5 ԪΪhelloȻٶȡ λΪ5 Ԫصֵ 19ҳ
  • 20. ? ArrayTester2 main() һ 5 x 10 x 15 飬 λΪ[3][5][10] ԪصֵΪ37 20ҳ
  • 21. ? ֪JavaиObject class Java classesļ̳иԴ ӦJava classбд methodshashCode()equals() clone()toString()getClass()ȡ getClass()һClass object 21ҳ
  • 22. ? Class classʮ⡣һclassesһ̳ ObjectʵԱJavaʱclasses interfacesҲenumarrayprimitive Java types ? boolean, byte, char, short, int, long, float, doubleԼؼvoidһclassأ򵱼 class loaderdefineClass()JVMã JVM ԶһClass objectɡ Java׼Դ롱۲Class objectʵʱ Classconstructorһprintln() ܹΪClassûpublic constructor 22ҳ
  • 23. ? ClassReflectionԴκ̽ classΨΪһClass objectܾɺ߻Ϊʮ Reflection APIs 23ҳ
  • 24. ? JavaǴӶ;Ϊһclass ӦClass object 24ҳ
  • 25. 25ҳ
  • 26. ? ɶʵ壬Reflection ̬ һԡԱctorһԡ ctorõǡctorͱ 鷳ЩٵClassnewInstance() ǵConstructor newInstance() ׼һClass[]ΪctorIJָͣ ? ΪһdoubleһintȻԴΪԱ getConstructor()һרctor ׼һObject[] Ϊctorʵֵָ 3.14159125רctor newInstance() 26ҳ
  • 28. 28ҳ
  • 29. ? á֮ctor൱ơ׼ һClass[]Ϊָͣһ StringһHashtableȻԴΪԱ getMethod()ضMethod object׼ һObject[]ԱȻ֮ض Method objectinvoke() ? ΪʲôMethod objectʱָطͣ 29ҳ
  • 30. ? Ϊmethod overloadingҪ signatureΨһطͲ signatureһɷݡ仰˵ֻҪָ methodƺͲУһָ һһ޶method 30ҳ
  • 31. 31ҳ
  • 32. ? ǰȣfieldݡ ɶˣΪҪԱ ȵClassgetField()ָfield ơضField objectֱ֮ Fieldget()set() 32ҳ
  • 33. 33ҳ